swift序列化和反序列化
枚舉和數組的序列化都有示範
基礎類
enum Style:Int{ StyleA = 0 StyleB = 1 } Class A:NSObject{ var style:Style? var num:Int? var str:String? var arr:Array? }
序列化
- 編碼成ojbect 如下
func encodeWithCoder(aCoder:NSCoder!){
aCoder.encode(self.style.rawValue,forKey:"style")
aCoder.encode(self.num,forKey:"num")
aCoder.encode(self.str,forKey:"str")
aCoder.encode(self.arr,forKey:"arr")
}
反序列化
init(coder aDecoder:NSCoder!){
self.style = Style(rawValue: aDecoder.decodeInteger(forKey:"style"))
self.num = aDecoder.decodeInteger(forKey:"num")
self.str = aDecoder.decodeObject(forKey:"str")
self.arr = aDecoder.decodeObject(forKey:"arr")
}