将数据存储到NSUserDefaults [英] Storing data to NSUserDefaults
问题描述
在我的iPhone应用程序中,我有一个名为联系
的课程,其中包含一个 ABRecordRef
作为参考特定联系人。
In my iPhone app, I have a class called Contact
which consists of an ABRecordRef
to serve as a reference to a particular contact.
我需要在 NSUserDefaults
中存储这些联系人的群组,但事情不起作用很好,因为联系
是一个自定义类。
I need to store groups of these contacts in NSUserDefaults
, but things aren't working out so well since Contact
is a custom class.
在这种情况下该怎么办?
Any ideas of what to do in this case?
推荐答案
您不能将 NSUserDefaults
用于自定义类。来自文档:
You cannot use NSUserDefaults
for a custom class. From the documentation:
NSUserDefaults
类提供了访问$ b $的便捷方法b常见类型,例如浮点数,双精度数,整数,布尔值和URL。
默认对象必须是属性列表,即(或
表示集合实例的组合):NSData
,NSString
,
NSNumber
,NSDate
,NSArray
,或NSDictionary
。如果你想存储任何
其他类型的对象,你通常应该将其存档以创建$ b $ c>NSData
的实例。
The
NSUserDefaults
class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of):NSData
,NSString
,NSNumber
,NSDate
,NSArray
, orNSDictionary
. If you want to store any other type of object, you should typically archive it to create an instance ofNSData
.
尝试使用 NSData
。例如,要将自定义对象加载到数组中,您可以执行
Try using NSData
. For example, to load custom objects into an array, you can do
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
objectArray = [[NSMutableArray alloc] init];
}
要存档数据,请使用:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"savedArray"];
只要你的自定义对象符合 NSCoding
协议:
This will all work so long as your custom object complies with the NSCoding
protocol:
- (void)encodeWithCoder:(NSCoder *)coder;
{
[coder encodeObject:label forKey:@"label"];
[coder encodeInteger:numberID forKey:@"numberID"];
}
- (id)initWithCoder:(NSCoder *)coder;
{
self = [[CustomObject alloc] init];
if (self != nil)
{
label = [coder decodeObjectForKey:@"label"];
numberID = [coder decodeIntegerForKey:@"numberID"];
}
return self;
}
ABRecord
是一个opaque C类型,因此它不是Objective-C意义上的对象。这意味着您无法扩展它,您无法在其上添加类别,您无法发送消息。您唯一能做的就是调用 ABRecord
中引用的函数,并使用 ABRecord
作为参数。
ABRecord
is an opaque C type, so it's not an object in the sense of Objective-C. That means you can not extend it, you can not add a category on it, you can not message it. The only thing you can do is call functions described in ABRecord
Reference with the ABRecord
as a parameter.
您可以做两件事来保持 ABRecord
所引用的信息:
You could do two things to be able to keep the information referenced by the ABRecord
around:
-
通过<获取
ABRecord
sid
code> ABRecordGetRecordID()。ABRecordID
定义为int32_t,因此您可以将其强制转换为NSInteger
并将其存储在您喜欢的任何位置。您可以稍后从ABAddressBookGetPersonWithRecordID()
或ABAddressBookGetGroupWithRecordID()
中获取记录。但是,用户或其他应用程序可以同时更改甚至删除记录。
Get the
ABRecord
sid
byABRecordGetRecordID()
. TheABRecordID
is defined as int32_t so you can cast it to anNSInteger
and store it wherever you like. You can later get the record back fromABAddressBookGetPersonWithRecordID()
orABAddressBookGetGroupWithRecordID()
. However, the record could be changed or even deleted by the user or another app meanwhile.
将记录中的所有值复制到标准 NSObject
子类并使用上面讨论的 NSCoding
来存储它。当然,您将不会受益于用户可能对记录所做的更改或添加。
Copy all values inside the record to a standard NSObject
subclass and use NSCoding
as discussed above to store it. You will then, of course, not benefit from changes or additions to the record the user could have made.
这篇关于将数据存储到NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!