{
success = 1;
user = {
"authentication_token" = "yyyyy";
"created_at" = "2014-06-11T13:22:43Z";
email = "yyyy@gmail.com";
id = 49;
name = Taro;
"updated_at" = "2014-06-13T12:49:07Z";
};
}
API Response handling
func connectionDidFinishLoading(connection: NSURLConnection!) {
var error: NSError? = nil
var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(self.responseData, options: NSJSONReadingOptions.MutableLeaves, error: &error)
if (error != nil) {
self.callback(nil, error)
}
if let resultObj = json as? Dictionary<String, AnyObject> {
if let userObj = resultObj["user"]? as? Dictionary<String, AnyObject> {
if let user = User.createFromJson(userObj) {
self.callback(user, nil)
return
}
}
}
self.callback(nil, nil)
}
create instance from JSON
class func createFromJson(userObj: Dictionary<String, AnyObject>) -> User! {
if let id = userObj["id"]? as? Int {
if let name = userObj["name"]? as? String {
if let email = userObj["email"]? as? String {
if let token = userObj["authentication_token"]? as? String {
return User(id:String(id), name:name, email:email, token:token)
}
}
}
}
return nil
}