Using if statements in Swift?

What is the best way to execute code if any additional parameter is non-zero? Obviously, the most obvious way to do this would be to directly check if nil is equal:

if optionalValue != nil {
     //Execute code
}

I also saw the following:

if let _ = optionalValue {
    //Execute code
}

The second seems to be more likely to be fast, since it uses an optional chain. Are there any advantages to using one method over another? Is there a better way to do this? For the better, I mean "more according to Swift," whatever that means.

+4
source share
1 answer

, . , , . , swift 1.2 :

if let unwrapped = optional {
    println("Use the unwrapped value \(unwrapped)")
}

(, Optional<T> ):

switch optional {
case .Some(let unwrapped):
    println("Use the unwrapped value \(unwrapped)")
case .None:
    break
}

, . , - , - .

, , , , , -, , .

+7

All Articles