Orkhan Alizade
Orkhan Alizade

Reputation: 7549

Checking anyobject value in Swift

With the following piece of code:

let decoded = NSJSONSerialization.JSONObjectWithData(data as! NSData, options: NSJSONReadingOptions.MutableContainers, error:  nil) as! [String:AnyObject]

I get [latitude: 40.3654922, won: 1, longitude: 49.9384457, winnerID: 552e].

And now I want to check if won object equal to '1' do something. But I cannot check the value of the object. I s any way to do it?

Upvotes: 1

Views: 337

Answers (2)

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

With swift 1.2, you can do:

if let won = decoded["won"] as? Int where won == 1 {
    // do something
}

Take a look at swift 1.2 release note (included in Xcode 6.3 release notes), we find that if let now support multiple condition check. For example:

if let a = foo(), b = bar() where a < b,
   let c = baz() {
}

or

if someValue > 42 && someOtherThing < 19, let a = getOptionalThing() where a > someValue {
}

Very powerful!

Upvotes: 4

sketchyTech
sketchyTech

Reputation: 5906

if let won = decoded["won"] as? Int {
    if won == 1 {
        // do something
    }
}

Upvotes: 3

Related Questions