mardi 27 janvier 2015

Why are multiple unwrapping optionals impossible?

I've been playing with optionals in swift. I make frequent use of the conditional unwrap pattern:



var myOptional: AnyObject?
if let unwrapped = myOptional {
// do stuff
}


However, on occasion I have two optional values that I only want to use if both of them are non-nil. As such, I tried to use the following syntax:



var myOptional: AnyObject?
var myOtherOptional: AnyObject?
if let unwrapped = myOptional && let otherUnwrapped = myOtherOptional? {
// do stuff
}


I've tried putting the two parts in brackets etc. but there doesn't seem to be a way to do this. Is there a good reason why I shouldn't be able to do it? Obviously I can just embed one statement in the other but I would prefer to put it all on one line.


Aucun commentaire:

Enregistrer un commentaire