0
votes

I have a model class User that I want to save in UserDefaults

import UIKit

class User: NSObject {

    var name:String!
    var email:String!
    var userId:String!
    var phone:String!
    var admin_status:String!
    var social_code:String!
    var token:String!
    var otp:String!
    var forget_otp:String!
    var p_img:String!
    var created:String!
    var status:String!

    static var currentUser:User = User()

    override init() {
        super.init()
    }

    required init(coder aDecoder: NSCoder) {
        self.name = aDecoder.decodeObject(forKey: "name") as! String
        self.email = aDecoder.decodeObject(forKey: "email") as! String
        self.userId = aDecoder.decodeObject(forKey: "userId") as! String
        self.phone = aDecoder.decodeObject(forKey: "phone") as! String
        self.admin_status = aDecoder.decodeObject(forKey: "admin_status") as! String
        self.social_code = aDecoder.decodeObject(forKey: "social_code") as! String
        self.token = aDecoder.decodeObject(forKey: "token") as! String
        self.otp = aDecoder.decodeObject(forKey: "otp") as! String
        self.forget_otp = aDecoder.decodeObject(forKey: "forget_otp") as! String
        self.p_img = aDecoder.decodeObject(forKey: "p_img") as! String
        self.created = aDecoder.decodeObject(forKey: "created") as! String
        self.status = aDecoder.decodeObject(forKey: "status") as! String
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encode(name, forKey: "name")
        aCoder.encode(email, forKey: "email")
        aCoder.encode(userId, forKey: "userId")
        aCoder.encode(phone, forKey: "phone")
        aCoder.encode(admin_status, forKey: "admin_status")
        aCoder.encode(social_code, forKey: "social_code")
        aCoder.encode(token, forKey: "token")
        aCoder.encode(otp, forKey: "otp")
        aCoder.encode(forget_otp, forKey: "forget_otp")
        aCoder.encode(p_img, forKey: "p_img")
        aCoder.encode(created, forKey: "created")
        aCoder.encode(status, forKey: "status")
    }

}

Code to save and get from UserDefaults

class func setUserDefault(ObjectToSave : AnyObject?  , KeyToSave : String)
    {
        let defaults = UserDefaults.standard

        if (ObjectToSave != nil)
        {

            defaults.set(ObjectToSave, forKey: KeyToSave)
        }

        UserDefaults.standard.synchronize()
    }

    class func getUserDefault(KeyToReturnValye : String) -> AnyObject?
    {
        let defaults = UserDefaults.standard

        if let name = defaults.value(forKey: KeyToReturnValye)
        {
            return name as AnyObject
        }
        return nil
    }

Saving

let user:User = User()
                    user.name = json["data"]["first_name"].string
                    user.email = json["data"]["email"].string
                    user.phone = json["data"]["phone"].string
                    user.social_code = json["data"]["social_code"].string
                    user.admin_status = json["data"]["admin_status"].string
                    user.otp = json["data"]["otp"].string
                    user.forget_otp = json["data"]["forget_otp"].string
                    user.p_img = json["data"]["p_img"].string
                    user.status = json["data"]["status"].string
                    user.userId = json["data"]["id"].string
                    user.created = json["data"]["created"].string

                    Utilities.setUserDefault(ObjectToSave: user, KeyToSave: "user") 

I also tried this

let encodedData = NSKeyedArchiver.archivedData(withRootObject: user)
                    UserDefaults.standard.set(encodedData, forKey: "User")

but it crashes because of static var currentUser:User = User()

how to fix this ?

2
what have you tried till now. Did you try NSCodingShubham Naik
I write the code that I am using but I don't know what to do with static var currentUser:User = User()Varun Naharia
As you can see I already tried theseVarun Naharia

2 Answers

2
votes

As for NSCoding: you have wrong method signature. Change

func encodeWithCoder(aCoder: NSCoder)

to

func encode(with aCoder: NSCoder)

Also, your object should explicitly conform to the NSCoding protocol:

class User: NSObject, NSCoding {...}

As for UserDefaults, you can not store custom objects in the UserDefaults, only NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary (or similar types in Swift), see documentation

1
votes

Have you tried by replacing your this line,

static var currentUser:User = User()

with this line,

static var currentUser:User!