如何在NSUserDefaults中保存NSMutableArrays [英] How to save NSMutableArrays in NSUserDefaults

查看:104
本文介绍了如何在NSUserDefaults中保存NSMutableArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个带有单个数据的NSmutablearrays.

i have 4 NSmutablearrays with individual data.

现在,我需要将它们分别保存在NSuserdefaults中,并分别进行检索.

Now i need to save them in NSuserdefaults individually and retrieve them individually.

我该怎么做

任何人都可以发布一些代码.

can any one please post some code.

预先感谢你.

推荐答案

数组的内容是什么?尽管NSUserDefaults可以存储数组,但它仅适用于包含NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary的数组.如果您的数组中还有其他内容(自定义对象或除上述对象之外的其他Cocoa对象),您就不能直接将其放入NSUserDefaults中.

What are the contents of the arrays? While NSUserDefaults can store arrays, it only works for arrays that contain NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If there's anything else in your array (custom objects, or other Cocoa objects besides the ones mentioned above) you can't just put it straight into NSUserDefaults.

假设您的对象正确,则可以通过执行以下操作来存储数组

Assuming your objects are right, you can store the array by doing

[[NSUserDefaults standardUserDefaults] setObject:yourArray forKey:@"aKey"];

您可以使用arrayForKey:方法再次将其取出.请注意,这样做时,它将不再是可变数组:您可以在返回的数组上调用mutableCopy以获得可变数组.此外,如果将数组存储在NSUserDefaults中之后对其进行了变异,则所做的更改将不会反映在应用程序的首选项中.

You can get it back out again with the arrayForKey: method. Note that when you do, it will no longer be a mutable array: you can call mutableCopy on the returned array to get a mutable one. Furthermore, if you mutate the array after storing it in NSUserDefaults, your changes will not be reflected in the app's preferences.

如果您有不会放入NSUserDefaults的对象数组(即不在上面的列表中),则不会丢失所有对象:您需要查看

If you've got an array of objects that won't go into NSUserDefaults (i.e. not on the list above), all is not lost: you need to look into NSCoding and archiving.

这篇关于如何在NSUserDefaults中保存NSMutableArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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