|
Can somebody clear something up about implicitly unwrapped optionals for me? Running the code
prints:
In the description of implicitly unwrapped optionals it says that:
In the documentation it reads:
Why do the first two |
|
Swift's behavior here is surprising, but intentional: implicit unwrapped optionals are just regular optionals with a special flag set, but when you copy the optional that flag isn't copied. In your code, copying the IUO makes the copy into a regular optional, which is what you're seeing. |
|
I see, thank you for that explanation! 🙂 Then why does the very first |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Implicitly unwrapped optionals are still optionals, they just get to skip the compiler checks. |
|
|
|
I'm afraid I then don't understand what
really means then. I promise I'm not trying to be obtuse here.
The second example here looks to me like I do have to unwrap Maybe I don't quite understand what "using" a property actually refers to? 🤔 In my mind it reads as "get at the value held by the optional if there is one" but maybe I'm mistaken in that intuition? |
|
That's because it is an optional at runtime. Is is treated as a regular value at compile time. So if you do this:
The compile will complain on the second line. But if you do this:
The code will be compiled without errors, and you will see the error at run time. So if you use |
|
I know this can sound a little goofy. An Optional is a type, same as an Int or String. While we might casually say "optional string" or "optional int," that is shorthand for "an "Optional that may contain an Int" or an "Optional that may contain a String." Just as an Int or a String has limits on what they can contain, and rules for using them, so does an Optional. Remember, an Optional is its own unique type. The key rule with an Optional is that in order to use its contents, it has to be unwrapped. There are two main ways to do that: by copying its value into another non-optional variable with if-let or similar, or marking it as implicitly unwrapped (!), which means that its value will be directly accessed at runtime. Should that value be nil, the program will crash immediately. That is by design. It is the way an Optional type is intended to work. Remember that Swift is strict about types, when a variable is created, its type is fixed forever. So an Int is always an Int, and an Optional is always an Optional. So what use is an Implicitly Unwrapped Optional? Well they are useful when mixing Swift and Objective-C. Even if you are writing pure Swift, a lot of Objective-C code is called on your behalf. Your variables will often be passed to an Objective-C function. They are also useful for communicating your intent to someone reading the code later. If you mark an Optional with !, you're comminuicating that you planned that this variable will never be nil when its needed. |
|
Thank you for the responses! 🙂 I still feel like I'm getting mixed messages here, or I'm merely misunderstanding. @guseulalio is telling me:
@bobdel is telling me:
One states that an implicitly unwrapped optional behaves as a normal optional at runtime, the other says that an IUO's value will be always directly accessed at runtime (without needing explicit unwrapping), which then should also hold when using I do understand that optionals are a distinct data type, that part isn't where I'm struggling. What I'm wondering is how exactly implicitly unwrapped optionals are implemented, and how their behaviour differs exactly from that of common optionals. Maybe let me try a more concrete yes-or-no question: Is it true that the only case in which the behaviour of an implicitly unwrapped optional differs from the behaviour of a common optional is if I try to assign the implicitly unwrapped optional to a variable or constant¹ that's been explicitly declared with a non-optional type? ¹) By the way, is there a single general term in Swift that subsumes both 'variable' and 'constant'? I know 'property' is used sometimes to that effect, but it seems to only refer to a constant or variable that exists as part of a class or structure. |
|
I think part of the confusion here is not really over optionals, it’s over what When you call
Let’s go down that list, remembering that
So, the string
By the way, here’s the documentation of the Swift Evolution implementation when implicitly-unwrapped optionals changed to their current behavior, which is quite helpful. |
|
I would restate the quote above this way: One states that an implicitly unwrapped optional is an optional at runtime (True), the other says that an IUO's value will be always directly accessed at runtime (without needing explicit unwrapping) (True)
You can look directly at the Optional implementation in the Standard Library. Note the extension on line 267. When printed to the console, a type can have customized output. When you print an optional using Print(), it appears the same regardless if it is implicitly unwrapped or not. I hope this is clear, but if not ask. |
|
Thank you so much @bbatsell for that elaborate explanation, that did teach me a lot! 🙂 It seems that indeed my confusion stemmed primarily from the workings of |
|
This has been a very interesting thread. Coming from a core Java background (very C-like), I have been very light on my understanding of Optionals. This discussion helped solidify a good bit of that. Thanks all! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.