如何将自定义对象的数组列表存储到NSUserDefaults? [英] How to store array list of custom objects to NSUserDefaults?

查看:97
本文介绍了如何将自定义对象的数组列表存储到NSUserDefaults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Item
{
    var name: String
    var collectionId: Int

    init(name: String, collectionId: Int) {
        self.name = name
        self.collectionId = collectionId

    }
}

class ViewController:UIViewController
{
    var itemList = [Item]()

    func recievedResults()
    {
        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setObject(self.itemList, forKey: "myList")
    }        
}

我收到一个错误属性列表不能包含'CFType'类型的对象"

I get an error "property lists cannot contain objects of type 'CFType'"

如何存储itemList并将其获取到NSUserDefaults?

How do I store and get the itemList to NSUserDefaults?

推荐答案

您需要为自定义类实现协议NSCoding.请参见此快速示例.

You need to implement protocol NSCoding for your custom class. Please see this Swift example.

这篇关于如何将自定义对象的数组列表存储到NSUserDefaults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆