gpt4 book ai didi

ios - 为什么在 UITableViewController 中使用 NSCoding 会出错?

转载 作者:行者123 更新时间:2023-11-28 06:58:36 25 4
gpt4 key购买 nike

我使用 NSCoding 来存储数据持久化这是我的代码

class CenterRecordTableViewController: UITableViewController {

private var records:[ReplayListData] = []

override func encodeWithCoder(aCoder: NSCoder) {
super.encodeWithCoder(aCoder)
aCoder.encodeObject(self.records, forKey: "records")
}

required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
if let res = aDecoder.decodeObjectForKey("records") as? [ReplayListData] {
self.records = res
}
}

override func viewDidLoad() {
super.viewDidLoad()
loadLocalData()
if records.count > 0 {
getDataFromServer()
}
}

func loadLocalData(){
if let data = NSUserDefaults.standardUserDefaults().objectForKey("CenterRecords") as? NSData {
let obj = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! CenterRecordTableViewController
self.records = obj.records
}
}

func saveLocalData(){
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "CenterRecords")
NSUserDefaults.standardUserDefaults().synchronize()
}

func getDataFromServer(){
//get data from server
saveLocalData()
}

}

但是当我运行应用程序时,错误是 hppen in

aCoder.encodeObject(self.records, forKey: "records")

这是控制台输出:

2015-09-19 13:09:57.219 test[41108:3488786] *** NSForwarding: warning: object 0x182651f0 of class 'test.ReplayListData' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[test.ReplayListData replacementObjectForKeyedArchiver:]

编辑

这是ReplayListData的代码我去掉了其他变量

class ReplayListData {
var created:Int
init(a:Int){
self.created = a
}
}

EDIT2

我尝试继承 ReplayListData 中的 NSObject

class ReplayListData:NSObject {
var created:Int
init(a:Int){
self.created = a
}
}

应用程序也崩溃了,但是控制台发生了变化

[test.ReplayListData encodeWithCoder:]: unrecognized selector sent to instance 0x17173970
2015-09-19 13:34:14.407 test[41238:3492144] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[test.ReplayListData encodeWithCoder:]: unrecognized selector sent to instance 0x17173970'
*** First throw call stack:
(0x2431cd67 0x31dbdc77 0x2432222d 0x24320109 0x24251938 0x24fc0425 0x24fc162f 0x24fc1c11 0x24fc0425 0x258d30 0x258eb8 0x24fc0425 0x24fc67c1 0x2599c8 0x25fa58 0x28b328 0x1a8f9cb 0x1a8f9b7 0x1a93411 0x242e2c41 0x242e1361 0x2422e981 0x2422e793 0x2b5af051 0x27820981 0x312da8 0x32359aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

尝试像这样修改您的类 ReplayListData

class ReplayListData: NSObject {
var created:Int
init(a:Int){
self.created = a
}
}

关于ios - 为什么在 UITableViewController 中使用 NSCoding 会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32664597/

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