Swift (and Cocoa APIs) should not return implicitly unwrapped optional types
| Originator: | jimroepcke | ||
| Number: | rdar://17219760 | Date Originated: | 07-Jun-2014 07:36 PM |
| Status: | Duplicate of 17219047 (Open) | Resolved: | |
| Product: | Developer Tools | Product Version: | 6.0 (6A215l) |
| Classification: | Reproducible: | Always |
Summary:
The Swift Cocoa APIs are full of methods that return ! types, like AnyObject[]!, String!, NSData!, etc.
I contend that this is unsafe by definition. The book says “it can be safely assumed to have a value all of the time”, so callers can and will assume the return value is not nil. If it is, the program will crash.
If the callee will never return nil, it shouldn't use an optional return type.
If the callee might return nil, it should use a ? type.
Please replace implicitly unwrapped optional return types non-optional or wrapped optional types in the Cocoa API.
Steps to Reproduce:
func someFunction() -> String!
{
return nil
}
someFunction().uppercaseString
Expected Results:
I expect Swift code to be safe.
Actual Results:
This program crashes.
Version:
6.0 (6A215l)
Notes:
Configuration:
This always occurs
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Dup created
I just created a duplicate (http://openradar.appspot.com/18119727) to create more exposure, because I think this will cause a lot of unexpected crashes and should be addressed!