Re: Implicitly unwrapped optionals
Re: Implicitly unwrapped optionals
- Subject: Re: Implicitly unwrapped optionals
- From: Vincent <email@hidden>
- Date: Sat, 14 Jun 2014 22:03:38 +0200
> Le 14 juin 2014 à 21:45, Roland King <email@hidden> a écrit :
>
> My current understanding is that you can access their value directly, without using the 'if let' syntax or the explicit unwrap (!) operator, but you'll crash if the optional doesn't have a value when you do. You can also treat them like an optional and using 'if' on them tells you if they exist just like an optional does.
That’s seems fairly correct.
> var date : NSDate!
>
> First question is why use an implicitly unwrapped optional instead of just an optional? You still have to test them so what do you gain?
If my understanding is correct, the IUO (that’s shorter) is assumed to have a valid value once it has been initialized (it should never return to nil). Therefore, it’d be superfluous to test it each time you encounter it. You can therefore write:
var anotherReferenceOnDate : NSDate = date
directly, whereas with a regular optional you’d have to use the ! everywhere:
var anotherReferenceOnDate : NSDate = date!
> I assume the slide is being a little sloppy and that really means use the normal "if xxx" syntax for these which returns true if the value exists, as xxx == nil would surely attempt an unwrap and throw if the value didn't exist.
I guess so.
> Finally if you have an implicitly optional Bool, bbb, what does
>
> if bbb
>
> do? Does it return whether bbb has a value or does it return the unwrapped value of the Bool used in the if? I assume the former in which case how do you use an implicitly unwrapped optional bool in an if?
Good riddle. Did you try in a playground?
Vincent
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden