Implicitly unwrapped optional assign in Xcode 8
In Xcode 8 release version, i found a strange scene.
Here is the code,
let implicitlyUnwrappedOptionalString: String! = "implicitlyUnwrappedOptionalString" let foo = implicitlyUnwrappedOptionalString print(implicitlyUnwrappedOptionalString) print(foo)
and here is the result:
These above shows that when i assign a implicitly unwrapped optional to a variable without a explicit type, the type will be inferred to an optional type, not the type which it originally is, aka implicitly unwrapped optional.
My Xcode has been updated to 8. Anyone can verify the behavior in Xcode 7.x?
The change is due to the Swift version changing or the Xcode?
- UILongPressGestureRecognizer on UITableViewCell - double call
- Using CIFilter with AVFoundation (iOS)
- Xcode workspace with Unity3D as a sub-project?
- Core Data: NSCocoaErrorDomain=134040 Only Occurs On Real Phone, Not Simulator
- No Google Analytics Demographic data generated for iOS App
- Xcode 7 beta 2: LaunchScreen.storyboard could not open document
Solutions Collect From Internet About “Implicitly unwrapped optional assign in Xcode 8”
This is a consequence of SE-0054 Abolish ImplicitlyUnwrappedOptional type which has been implemented in Swift 3. Extract from that proposal (emphasis added):
However, the appearance of ! at the end of a property or variable declaration’s type no longer indicates that the declaration has IUO type; rather, it indicates that (1) the declaration has optional type, and (2) the declaration has an attribute indicating that its value may be implicitly forced. …
If the expression can be explicitly type checked with a strong optional type, it will be. However, the type checker will fall back to forcing the optional if necessary. The effect of this behavior is that the result of any expression that refers to a value declared as T! will either have type T or type T?. For example, in the following code:
let x: Int! = 5 let y = x let z = x + 0
… x is declared as an IUO, but because the initializer for y type checks correctly as an optional, y will be bound as type Int?. However, the initializer for z does not type check with x declared as an optional (there’s no overload of + that takes an optional), so the compiler forces the optional and type checks the initializer as Int.
In your case, the assignment
let foo = implicitlyUnwrappedOptionalString
foo a strong optional, as in the example
let y = x
from the proposal.
You could make
foo an IUO by adding an explicit type annotation
let foo: String! = implicitlyUnwrappedOptionalString
but generally you should try to get rid from IUOs in your code,
as stated in the same proposal:
Except for a few specific scenarios, optionals are always the safer bet, and we’d like to encourage people to use them instead of IUOs.
- why most of the objects we create in iphone are pointers
- 'Use of self in method call before super.init initializes self', can't init properties through a method call
- iOS – Is it possible to communicate between apps via localhost?
- app is crashing on second run through of loop with nsnull count
- Swift compiler error – While emitting SIL for 'tableView' at
- iPhone Apps running on iPod Touch
- Can you attach a UIGestureRecognizer to multiple views?
- UITextField causes crash in iOS 5, works fine in iOS 4
- What does it mean that string and character comparisons in Swift are not locale-sensitive?
- Following in app purchase, app crashing on startup. productIdentifier=nil?
- Difference between NSCalendarUnitWeekOfYear & NSCalendarUnitWeekOfMonth
- How do I refactor swift in Xcode?
- Best archiver library for iOS
- Resize UIView While It Is Rotated with Transform
- Setup Xcode and FLTK