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 ar
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.
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)
In this case, archive
will be an NSData
instance.
Update
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")
}
(Also consider naming your keys the same as their properties, like "alertCounter"
for alertCounter
.)