NSUserDefaults insert non-property list object

//You can only store property list types (array, data, string, number, date, dictionary) or urls in NSUserDefaults.
如果你想保存自定义的类,那么该类必须要实现NSCoding协议,并将该类转换成NSData,才能保持到NSUserDefaults
//Attempt to insert non-property list object {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{}

你可能感兴趣的:(NSUserDefaults insert non-property list object)