NSUserDefaults 与 SQLite3。 NSUSerDefaults 不可变?
请原谅,我知道这些问题已经有了答案。但我想知道我为实现选择 NSUserDefaults 的方法是否正确。
我正在尝试在 UITableView
上显示“最喜欢的列表”,其中我需要为每个列表保存大约 5 个对象。所以我想我的 tableView 数据源将在 NSArray
中包含 NSDictionary
。
当我读到“从 NSUserDefaults 返回的值是不可变的”时,我对使用 NSUserDefaults 感到惊讶。有没有办法可以操纵存储的对象?例如,从 NSUSerDefaults
读取数组,然后向其添加新对象并将其存储回来。
NSUserDefaults
看起来非常容易使用。
编辑 我们可以插入可变对象吗?
Pardon this, I know there's are answers out there for these question already. But I would like to know whether is my approach of choosing NSUserDefaults for my implementation correct.
I am trying to do a 'favourite list' to be displayed on UITableView
where I will need to save about 5 objects for each list. So I figured I will have NSDictionary
within a NSArray
for my tableView dataSource.
I was taken aback from using NSUserDefaults
when I read "Values returned from NSUserDefaults are immutable". Is there a way I can manipulate the objects stored? For example, read the array from NSUSerDefaults
then add new objects onto it and store it back.
NSUserDefaults
looks very easy to use.
EDIT
Can we insert mutable objects in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样从 NSUserDefaults 读取对象:
You can read object from NSUserDefaults like this:
如果对象是可变/不可变集群的一部分,例如 NSArray/NSMutableArray NSDictionary/NSImmutableDictionary 等,则可以调用 mutableCopy 来获取对象的副本。
因此,从字典中获取对象 - 调用mutableCopy 就可以修改它并保存回来。
请记住,此方法的名称中有一个副本,因此您需要在使用完毕后释放该副本。
You can call
mutableCopy
to get a copy of of an object if it is part of a Mutable/Immutable cluster such as NSArray/NSMutableArray NSDictionary/NSImmutableDictionary, etc.So get the object out of the dictionary - call mutableCopy on it and you can modify it and save it back.
Remember that this method has a copy in its name, so you need to release the copy when you are finished with it.