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

Posted

技术标签:

【中文标题】如何使用静态自我实例将模型类保存在 UserDefaults 中?【英文标题】:How to save model class in UserDefaults with static self instance? 【发布时间】:2017-05-30 05:04:12 【问题描述】:

我有一个模型类 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()而崩溃

如何解决这个问题?

【问题讨论】:

到目前为止您尝试过什么。你尝试过 NSCoding 我编写了我正在使用的代码,但我不知道如何处理static var currentUser:User = User() 试试this answer Or this one 如你所见,我已经尝试过这些 【参考方案1】:

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

func encodeWithCoder(aCoder: NSCoder)

func encode(with aCoder: NSCoder)

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

class User: NSObject, NSCoding ...

至于UserDefaults,你不能在UserDefaults中存储自定义对象,只有NSDataNSStringNSNumberNSDateNSArrayNSDictionary(或Swift中的类似类型) ),见documentation

【讨论】:

【参考方案2】:

您是否尝试过替换您的这一行,

static var currentUser:User = User()

用这条线,

static var currentUser:User!

【讨论】:

以上是关于如何使用静态自我实例将模型类保存在 UserDefaults 中?的主要内容,如果未能解决你的问题,请参考以下文章

django 在父子模型类之间保存实例

模型类(在 MVC 中)应该使用静态方法还是实例方法?

猫鼬模型静态/方法不保存“this”中的值

C ++中同一进程中的多个静态类实例

如何将 priority_queue 与类实例的非静态比较方法一起使用?

在java中,为啥类实例也能访问静态域?