dankogai / swift-json

Even Swiftier JSON Handler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Access Array Results

wattanak opened this issue · comments

import Foundation
class Response : JSON{
var key = ""
override init(_ obj:AnyObject){ super.init(obj)}
override init(_ json:JSON) { super.init(json)}
func setKey(key: String){
self.key = key;
}
var api_name :String? { return self[key]["api_name"].asString }
var status :Bool? { return self[key]["response"]["status"].asBool }
var code :Int? { return self[key]["response"]["code"].asInt }
var message :String? { return self[key]["response"]["message"].asString }
var num_rows :Int? { return self[key]["num_rows"].asInt }
var results :NSDictionary?{ return self[key]["response"]["results"].asDictionary}
var request:String? { return self[key]["request"].asString }
}

HERE is the JSON Result
Optional({
"checkEmailExist": {
"response": {
"num_rows": 1,
"status": true,
"code": 200,
"message": "Successful",
"results": {
"id": 1,
"firstname": "chantrea",
"lastname": "nhem",
"username": "chantrea",
"email": "chantrea@wattanak.com",
"phonenumber": 23523462346234,
"user_type": 1,
"status": "status",
"picture_url": "user_c071ebd35cc6d0ce0ec6b5e9ab1a2a04c795ab3be1b0f986a25042c6c4c7e160.jpg"
}
},
"request": []
}
})

             for(key, val) in response.results!{
                        if val as NSString == "chantrea"{
                            println("CISA")
                        }
                    }

always give me errors and crash the apps the errors message is swift dynamic cast fail

but if I do for the key that is working fine only the value I really have no idea why it is like that. Please help.