且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保存自定义结构的数组来迅速的plist

更新时间:2022-06-16 21:58:11

您是不是要叫连接codeWith codeR()自己。使用的NSKeyedArchiver NSKeyedUnarchiver 。在的init(codeR:)连接codeWith codeR()将被称为根据需要通过归档/取档。

You're not meant to call encodeWithCoder() yourself. Use NSKeyedArchiver and NSKeyedUnarchiver. The init(coder:) and encodeWithCoder() will be called as needed by the archiver / unarchiver.

NSCoding 兼容 alertData ​​ code>实例(这确实应该 AlertData ,因为类名应该是适当的情况下)到一个数组(或字典,如果你请),并做到这一点:

Place your NSCoding-compliant alertData instances (this should really be AlertData since class names should be proper case) into an array (or dictionary if you please) and do this:

let archive = NSKeyedArchiver.archivedDataWithRootObject(yourArrayOfAlertData)

在这种情况下,归档将是的NSData ​​ code>实例。

In this case, archive will be an NSData instance.

更新

其实,我想我错过了它的第一次,但你的连接code ...()方法是不正确的。在所有。考虑与区别:

Actually, I guess I missed it the first time, but your encode...() method isn't right. At all. Consider the difference with:

public func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeDouble(alertCounter, forKey:"Item1")
    aCoder.encodeObject(alertText, forKey:"Item2")
    aCoder.encodeObject(alertColor , forKey:"Item3")
    aCoder.encodeInteger(alertColorIndex, forKey:"Item4")
}

(也可以考虑命名你的钥匙一样它们的属性,如alertCounter alertCounter