I have made an API call to log my users in an app. i am using Alamofire and SwiftyJSON. API call works well and I get a JSON
file like this one :
{
"code": 200,
"message": "OK",
"data": {
"user_id": 1,
"user_email": "test",
"user_username": "kk1",
}
}
In order to save the user info in UserDefault
, I want to create my User object with this JSON
response. Here is the model
class LFUser: NSObject {
var user_id: Int?
var user_email: String?
var user_username: String?
init(dict: [String: AnyObject]){
super.init()
user_id = dict["user_id"] as? Int
user_email = dict["user_email"] as? String
user_username = dict["user_username"] as? String
}
}
Here is the part of the login function when the API call and the objectis created :
func login(userEmail: String, userPassword: String, finished: @escaping(_ status:String,_ data: LFUser?)-> ()) {
if let value = response.result.value {
let dict = JSON(value)
let code = dict["code"].intValue
let message = dict["message"].stringValue
if let data = dict["data"].dictionary {
print(data)
let user = LFUser(dict: data as [String : AnyObject])
print(user.user_email)
finished("Success", user)
}
}
The print(data) works well but there is a problem when creating the object and the print(user.user_email) display nil.