Devanshu Devanshu - 2 months ago 15
Swift Question

Update Stored User Model with api response (Using Gloss) without updating each keys line by line

I am using following class structure:

import Gloss

class LMSUser: BaseResponse {

public var userId:String?
public var firstName:String?
public var middleName:String?
public var lastName:String?
public var email:String?
public var phone:String?
public var token:String?
public var roles:[String]?

required init?(json: JSON) {
super.init(json: json)
self.userId = "userId" <~~ json
self.firstName = "firstName" <~~ json
self.middleName = "middleName" <~~ json
self.lastName = "lastName" <~~ json
self.email = "email" <~~ json
self.phone = "phone" <~~ json
self.token = "token" <~~ json
self.roles = "roles" <~~ json
self.postInit()
}

public func postInit() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.appUser = self
UserDefaults.standard.set(self.toJSON(), forKey: String.kDeviceUser)
UserDefaults.standard.synchronize()
}

override func toJSON() -> JSON? {
return jsonify([
super.toJSON(),
"userId" ~~> self.userId,
"firstName" ~~> self.firstName,
"middleName" ~~> self.middleName,
"lastName" ~~> self.lastName,
"email" ~~> self.email,
"phone" ~~> self.phone,
"token" ~~> self.token,
"roles" ~~> self.roles,
])
}
}


Say its a singleton class and has more variables in it and the api does not provide full detail each time.
What I do is just manually update each field line by line.
Can anybody help me to save little lines of my code?

Answer Source

You can have a method in your class say initUserData to initialise data which is available, and for lines you have write :)

Sample Code:

import Gloss

class LMSUser: BaseResponse {

    public var userId:String?
    public var firstName:String?
    public var middleName:String?
    public var lastName:String?
    public var email:String?
    public var phone:String?
    public var token:String?

    required init?(json: JSON) {
        super.init(json: json)
        self.initUserData(json: json)
        self.postInit()
    }

    public func initUserData(json: JSON) {
        let vUserID:String? = "userId" <~~ json
        if vUserID != nil {
            self.userId = vUserID
        }

        let vFirstName:String? = "firstName" <~~ json
        if vFirstName != nil {
            self.firstName = vFirstName
        }

        let vMiddleName:String? = "middleName" <~~ json
        if vMiddleName != nil {
            self.middleName = vMiddleName
        }

        let vLastName:String? = "lastName" <~~ json
        if vLastName != nil {
            self.lastName = vLastName
        }

        let vEmail:String? = "email" <~~ json
        if vEmail != nil {
            self.email = vEmail
        }

        let vPhone:String? = "phone" <~~ json
        if vPhone != nil {
            self.phone = vPhone
        }

        let vToken:String? = "token" <~~ json
        if vToken != nil {
            self.token = vToken
        }
    }

    public func postInit() {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.appUser = self
    }

    override func toJSON() -> JSON? {
        return jsonify([
            super.toJSON(),
            "userId" ~~> self.userId,
            "firstName" ~~> self.firstName,
            "middleName" ~~> self.middleName,
            "lastName" ~~> self.lastName,
            "email" ~~> self.email,
            "phone" ~~> self.phone,
            "token" ~~> self.token
            ])
    }
}