我有两个数组
var questions: [Question]!
var types: [[Type]]!
我正在尝试将这两个数组转换为NSData。我喜欢下面的
let typesData = NSKeyedArchiver.archivedDataWithRootObject(types)
let questionsData = NSKeyedArchiver.archivedDataWithRootObject(questions)
一旦遇到typesData,应用程序崩溃并抛出如下错误:
*** NSForwarding: warning: object 0x15e5a4100 of class '(app Name).Type' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[(app Name).Type replacementObjectForKeyedArchiver:]
我假设它导致错误,因为那些数组有自定义类型。有什么办法可以将这些数组转换为NSData吗?任何解决这个问题的解决方案都将受到赞赏。
答案 0 :(得分:1)
对于swift,你需要在Object(Question,Type)类中实现它,如下所示:
class ItemObject:NSObject, NSCoding {
var itemName = ""
var itemCheck = false
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(itemName, forKey:"itemName")
aCoder.encodeBool(itemCheck, forKey:"itemCheck")
}
required init (coder aDecoder: NSCoder) {
self.itemName = aDecoder.decodeObjectForKey("itemName") as! String
self.itemCheck = aDecoder.decodeBoolForKey("itemCheck")
}
override init() {
super.init()
}
}
对于NSUserDefaults
和CoreData