如何使用静态自我实例在UserDefaults中保存模型类?

时间:2017-05-30 05:04:13

标签: ios swift nskeyedarchiver userdefaults

我想要保存在User

中的模型类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")
    }

}

保存并从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
    }

保存

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") 

我也试过这个

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

但由于static var currentUser:User = User()

而崩溃了

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

至于NSCoding:您的方法签名错误。改变

func encodeWithCoder(aCoder: NSCoder)

func encode(with aCoder: NSCoder)

此外,您的对象应明确符合NSCoding协议:

class User: NSObject, NSCoding {...}

至于UserDefaults,您无法在UserDefaults中存储自定义对象,只能NSDataNSStringNSNumberNSDate,{{1} },或NSArray(或Swift中的类似类型),请参阅Webmaster Central Help Forum

答案 1 :(得分:1)

您是否尝试过更换此行,

static var currentUser:User = User()

有了这一行,

static var currentUser:User!