I am rather perplexed by this. If we take the method cellForRowAtIndexPath: in UITableView for example, it's method signature is:
func cellForRowAtIndexPath(_ indexPath: NSIndexPath!) -> UITableViewCell!
And its return value is:
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.
That sounds like the perfect reason to use a standard optional. In fact, since all pointer based types in Objective-C can be nil... it seems to make sense that all Objective-C pointer types should be imported as standard optionals.
I know from the WWDC talk that they say that for implicitly unwrapped optionals:
- Can be tested explicitly for nil
- Can directly access properties/methods of the underlying value
- Can be implicitly converted to its underlying value
And from Apple's Using Swift with Cocoa and Objective-C:
When you access the value in this kind of optional type without safely unwrapping it first, the implicitly unwrapped optional checks whether the value is missing. If the value is missing, a runtime error occurs.
So, instead of importing a possibly nil value into Swift as an optional, they decided to import it as something that states that this should never be nil... but could be? It sounds like they completely negated the safety of the optional type in Swift for Objective-C APIs by doing this. What do I seem to be missing?
Instead of giving a compile time error or warning, they decided a runtime error was better? This is very confusing.
Considering that nothing seems to answer this question that I have seen... I am thinking it is something obvious to everybody else that I am just not seeing but... Why is it like this?
Is it really just to save people from using if let or optional chaining when they use Objective-C APIs in Swift, or something more?