将数据存储到 NSUserDefaults
在我的 iPhone 应用程序中,我有一个名为 Contact
的类,它由一个 ABRecordRef
组成,用作对特定联系人的引用。
我需要将这些联系人组存储在 NSUserDefaults
中,但由于 Contact
是一个自定义类,所以事情并没有那么顺利。
在这种情况下有什么想法吗?
In my iPhone app, I have a class called Contact
which consists of an ABRecordRef
to serve as a reference to a particular contact.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能将
NSUserDefaults
用于自定义类。从文档中:尝试使用 NSData。例如,要将自定义对象加载到数组中,您可以执行以下操作:
要归档数据,请使用:
只要您的自定义对象符合
NSCoding
协议,这一切都可以工作:ABRecord 是一种不透明的 C 类型,因此它不是 Objective-C 意义上的对象。这意味着您无法扩展它,无法在其上添加类别,也无法向其发送消息。您唯一能做的就是使用
ABRecord
作为参数来调用ABRecord
参考中描述的函数。您可以做两件事来保留
ABRecord
引用的信息:通过
获取
。ABRecord
的id
>ABRecordGetRecordID()ABRecordID
定义为 int32_t,因此您可以将其转换为NSInteger
并将其存储在您喜欢的任何位置。您稍后可以从ABAddressBookGetPersonWithRecordID()
或ABAddressBookGetGroupWithRecordID()
取回记录。但是,该记录可能会被用户或其他应用程序更改甚至删除。将记录内的所有值复制到标准
NSObject
子类,并使用上面讨论的NSCoding
来存储它。当然,您将无法从用户对记录所做的更改或添加中受益。You cannot use
NSUserDefaults
for a custom class. From the documentation:Try using
NSData
. For example, to load custom objects into an array, you can doTo archive the data, use:
This will all work so long as your custom object complies with the
NSCoding
protocol: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 inABRecord
Reference with theABRecord
as a parameter.You could do two things to be able to keep the information referenced by the
ABRecord
around: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.Copy all values inside the record to a standard
NSObject
subclass and useNSCoding
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.好吧,Apple 的建议 是存储记录标识符、名字和姓氏。然后,您可以尝试通过标识符从地址簿中检索联系人,如果未找到记录或者该记录不是正确的人,请尝试通过名字和姓氏进行检索(因为记录标识符可能会根据您的记录来源而改变)地址簿数据)。
这可能是您想要的,也可能不是,具体取决于您存储数据的原因。但是,您可以很容易地将这三个值放入 NSDictionary 并将字典写入
NSUserDefaults
。Well, Apple's recommendation is to store the record identifier, the first name, and the last name. You can then try retrieving the contact from the address book by the identifier and, if the record isn't found or if it's not the right person, try retrieving by first and last name (since record identifiers may change depending on the source of your address book data).
This may or may not be what you want, depending on why you're storing the data. But, you could pretty easily put those three values into an NSDictionary and write the dictionary to
NSUserDefaults
.ABRecordRef 似乎是一个 const void * ,因此将其存储在 NSUserDefaults 中,您必须将其包装在 NSData 中:
NSData *d = [[NSData alloc] initWithBytes:thePointer length:sizeof(ABRecordRef)];
。ABRecordRef appears to be a
const void *
, so store it inNSUserDefaults
, you have to wrap it inNSData
:NSData *d = [[NSData alloc] initWithBytes:thePointer length:sizeof(ABRecordRef)];
.您可以使用转换为 vCard 表示形式实际存储它,即 CFStringRef,可以轻松用作 NSString。
You can store it actually using conversion to vCard representation, which is CFStringRef, that can be easily used as NSString.