gpt4 book ai didi

swift - Swift 3 中的 NSCoding - 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,原因 : '-[AppName. User encodeWithCoder:]

转载 作者:搜寻专家 更新时间:2023-11-01 07:15:01 25 4
gpt4 key购买 nike

我在实现 NSCoding 时遇到了问题。这是我的用户类代码:

public class User: NSCoder {
...

NSCoding 方法:

解码器:

required public init(coder aDecoder: NSCoder) {
self.deviceToken = aDecoder.decodeObject(forKey: kUserDeviceTokenKey) as? String
self.postalCode = aDecoder.decodeObject(forKey:kUserPostalCodeKey) as? String
self.latitude = aDecoder.decodeObject(forKey:kUserLatitudeKey) as? Float
self.views = aDecoder.decodeObject(forKey:kUserViewsKey) as? Int
...

编码器:

func encode(with aCoder: NSCoder) {
aCoder.encode(deviceToken, forKey: kUserDeviceTokenKey)
aCoder.encode(postalCode, forKey: kUserPostalCodeKey)
aCoder.encode(latitude, forKey: kUserLatitudeKey)
aCoder.encode(views, forKey: kUserViewsKey)
...

保存和加载方法:

public func save(user : User){
let encodedUser = NSKeyedArchiver.archivedData(withRootObject: user)
UserDefaults.standard.set(encodedUser, forKey: kUserLogedKey)
}

public func getUserLoged() -> User?{
if let userData = UserDefaults.standard.object(forKey: kUserLogedKey),
let user = NSKeyedUnarchiver.unarchiveObject(with: userData as! Data) as? User{
print("DICT USER LOGED: \(user.dictionaryRepresentation())")
return user
}else {
return nil
}
}

我收到以下错误:

-[App.User encodeWithCoder:]: unrecognized selector sent to instance 0x101946e00
2017-03-05 14:22:47.540798 App[2642:750699] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[.User encodeWithCoder:]: unrecognized selector sent to instance 0x101946e00'

最佳答案

感谢@vadian 的解决方案:

我替换了:public class User: NSCoderby: 公共(public)类 User: NSObject, NSCoding

关于swift - Swift 3 中的 NSCoding - 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,原因 : '-[AppName. User encodeWithCoder:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608969/

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