美文网首页
NSUserDefaults insert non-proper

NSUserDefaults insert non-proper

作者: 半江瑟瑟 | 来源:发表于2017-08-03 15:17 被阅读32次

//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 <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{}

相关文章

网友评论

      本文标题:NSUserDefaults insert non-proper

      本文链接:https://www.haomeiwen.com/subject/rleplxtx.html