如何保存自定义结构的数组来迅速的plist [英] How to save an array of custom structs to plist swift
问题描述
我想报警数据保存到一个plist中,它是类alertData数组的形式,所有的信息,我可以找点编码,然后把它在阵列中,但我中号困惑,这是什么呢?我也无法弄清楚如何做到这一点,这是我的游乐场:任何帮助将是巨大的。
I'm trying to save alert data to a plist, it is in the form of an array of the class alertData, all the info i can find points to encoding it, and then putting it in the array, but i'm confused as to what this does? And i also can't figure out how to do it, here's my playground: Any help would be great
类(我试图挽救这些数组)
The class (i'm trying to save an array of these)
public class alertData: NSObject, NSCoding {
//Properties of the timer data - make optionals optional i.e. alert count etc.
var alertCounter: NSTimeInterval?
var alertText: String?
var alertColor: UIColor?
var alertColorIndex: Int?
//initialisation
init(alertCounter: Double, alertText: String, alertColor: UIColor, alertColourIndex: Int) {
self.alertCounter = alertCounter
self.alertText = alertText
self.alertColor = alertColor
self.alertColorIndex = alertColourIndex
}
public required init(coder aDecoder: NSCoder) {
alertCounter = aDecoder.decodeDoubleForKey("Item1")
alertText = aDecoder.decodeObjectForKey("Item2") as? String
alertColor = aDecoder.decodeObjectForKey("Item3") as? UIColor
alertColorIndex = aDecoder.decodeIntegerForKey("Item4")
}
public func encodeWithCoder(aCoder: NSCoder) {
alertCounter = aCoder.decodeDoubleForKey("Item1")
alertText = aCoder.decodeObjectForKey("Item2") as? String
alertColor = aCoder.decodeObjectForKey("Item3") as? UIColor
alertColorIndex = aCoder.decodeIntegerForKey("Item4")
}
}
数组(当我在应用程序中添加警报数据变得填充):
The array (becomes populated when i add alert data in the app):
var alertDataSource: [alertData] = []
和为en code是:
And to encode it:
let archive = NSKeyedArchiver.archivedDataWithRootObject(self.alertDataSource)
print(archive)
和提取它:
let result = NSKeyedUnarchiver.unarchiveObjectWithData(archive)
print(result)
在此先感谢任何提示/咨询
Thanks in advance for any tips / advice
推荐答案
您是不是要叫连接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
)
这篇关于如何保存自定义结构的数组来迅速的plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!