Get a negative extra chain

This is not valid (bad expression)

if !(let nsDictionaryObject = swiftObject as? NSDictionary)
{
    "Error could not make NSDictionary in \(self)"
    return
}

      

Is it possible to check for negation of Option Chain expression on 1 line?

+2


source to share


3 answers


In Swift 2.0, you can use

guard let nsDictionaryObject = swiftObject as? NSDictionary else {
    "Error could not make NSDictionary in \(self)"
    return
}

      



This will also bind nsDictionaryObject

to scope outside of the guard statement.

+9


source


If you are using swift 1.2 you can do it like this:



if let nsDictionaryObject = swiftObject as? NSDictionary{} else {

    //your code

}

      

0


source


In Swift 1, you can for example use a branch else

or check out the result nil

.

var swiftObject: AnyObject = ""

if let _ = swiftObject as? NSDictionary { } else {
    println("error")
}

if swiftObject as? NSDictionary == nil {
    println("error")
}

      

0


source







All Articles