struct AllItemsData {
var DSTBID: String!
var CCAS: String!
var BCAS: String!
}
这是我从中创建类型数组的结构
AllItemsDataArray = [AllItemsData()]
添加一些数据后,现在我想将其存储到用户默认值中。
我是这样做的:
AllItemsDataArray.removeFirst()
let archivedArray = NSKeyedArchiver.archivedData(withRootObject: All_ItemsDataArray)
print(archivedArray)
preference.set(archivedArray, forKey: allItemsDataKey)
但错误就像:
ios [1540:537869] - [_ SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例0x15d1e200
发生崩溃之前数组中的值是:
AllItemsData(DSTBID:GGGGGGGGGGGG,CCAS:,BCAS:)
AllItemsData(DSTBID:HHHHHHHHHHHH,CCAS:,BCAS:)
答案 0 :(得分:2)
您需要为您的对象实施NSCoding协议,并使用class
代替struct
class AllItemsData: NSObject, NSCoding {
var DSTBID: String!
var CCAS: String!
var BCAS: String!
required convenience init?(coder decoder: NSCoder) {
let DSTBID = decoder.decodeObjectForKey("DSTBID") as? String,
let CCAS = decoder.decodeObjectForKey("CCAS") as? String,
let BCAS = decoder.decodeObjectForKey("BCAS") as? [String]
self.init(
DSTBID: DSTBID,
CCAS: CCAS,
BCAS: BCAS
)
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.DSTBID, forKey: "DSTBID")
coder.encodeObject(self.CCAS, forKey: "CCAS")
coder.encodeInt(Int32(self.BCAS), forKey: "BCAS")
}
}