For Swift 4.1 (tested code)
import UIKit
import SwiftyJSON
class UserObject: NSObject, NSCoding {
var username: String? = ""
var userID: String? = ""
var user_email: String? = ""
var name: String? = ""
var age: String? = ""
var gender: String? = ""
override init() {
super.init()
}
init(dictionary: JSON) {
//User data initialize...
username = dictionary["username"].stringValue
userID = dictionary["iUserID"].stringValue
user_email = dictionary["email"].stringValue
name = dictionary["name"].stringValue
age = dictionary["age"].stringValue
gender = dictionary["gender"].stringValue
}
required public init(coder aDecoder: NSCoder) {
username = aDecoder.decodeObject(forKey: "username") as? String
userID = aDecoder.decodeObject(forKey: "iUserID") as? String
user_email = aDecoder.decodeObject(forKey: "email") as? String
name = aDecoder.decodeObject(forKey: "name") as? String
age = aDecoder.decodeObject(forKey: "age") as? String
gender = aDecoder.decodeObject(forKey: "gender") as? String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(username, forKey: "username")
aCoder.encode(userID, forKey: "iUserID")
aCoder.encode(user_email, forKey: "email")
aCoder.encode(name, forKey: "name")
aCoder.encode(age, forKey: "age")
aCoder.encode(gender, forKey: "gender")
}
}
Note : NSCoding protocol is important