//You can only store property list types (array, data, string, number, date, dictionary) or urls in NSUserDefaults.
如果你想保存自定義的類,那么該類必須要實(shí)現(xiàn)NSCoding協(xié)議,并將該類轉(zhuǎn)換成NSData,才能保持到NSUserDefaults
//Attempt to insert non-property list object <NSIndexPath: 0xc000000000e00016> {length = 2, path = 0 - 7} for key selectSuCaiKey'
存
let selectSuCaiValue : NSIndexPath = indexPath
NSUserDefaults.standardUserDefaults().setValue(NSKeyedArchiver.archivedDataWithRootObject(selectSuCaiValue), forKey: "selectSuCaiKey")
NSUserDefaults.standardUserDefaults().synchronize()
取
if let data : NSData = NSUserDefaults.standardUserDefaults().valueForKey("selectSuCaiKey") as? NSData {
if let selectSuCaiValue = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSIndexPath {
if(selectSuCaiValue.isEqual(indexPath)){
self.cardView?.backgroundColor = UIColor.redColor()
}else{}
}else{}
}else{}