Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare value in swift optional

Tags:

swift

optional

var someString: String? = "a"    
someString = "b" 
// or someString = nil

Condition: someString is not nil and "a"

Example:

if someString != nil && someString != "a" {

}

Is it possible to condition a single?

like image 345
jjaeko Avatar asked Aug 25 '14 15:08

jjaeko


1 Answers

The correct way in Swift 3 is to use a multi-clause condition:

if let bString = someString, bString != "a" {
    print("bString: '\(bString)' is not nil and is different from 'a'")
}

https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md

like image 117
lkraider Avatar answered Oct 07 '22 16:10

lkraider