gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:54:52 26 4
gpt4 key购买 nike

我有一个模型类 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:你的方法签名有误。改变

func encodeWithCoder(aCoder: NSCoder)

func encode(with aCoder: NSCoder)

此外,您的对象应明确遵守 NSCoding 协议(protocol):

class User: NSObject, NSCoding {...}

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

关于ios - 如何使用静态自身实例将模型类保存在 UserDefaults 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com