将数据存储到 NSUserDefaults [英] Storing data to NSUserDefaults

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

问题描述

在我的 iPhone 应用程序中,我有一个名为 Contact 的类,它由一个 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 中,但由于 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?

推荐答案

您不能将 NSUserDefaults 用于自定义类.来自文档:

You cannot use NSUserDefaults for a custom class. From the documentation:

NSUserDefaults 类提供了方便的方法来访问常见类型,例如浮点数、双精度数、整数、布尔值和 URL.一个默认对象必须是一个属性列表,即(或对于集合实例的组合):NSDataNSStringNSNumberNSDateNSArrayNSDictionary.如果您想存储任何其他类型的对象,您通常应该将其归档以创建一个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, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

尝试使用 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 是一个不透明的 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:

  1. 通过ABRecordGetRecordID()获取ABRecordid.ABRecordID 定义为 int32_t,因此您可以将其转换为 NSInteger 并将其存储在您喜欢的任何位置.您稍后可以从 ABAddressBookGetPersonWithRecordID()ABAddressBookGetGroupWithRecordID() 取回记录.但是,用户或其他应用程序可能会同时更改甚至删除记录.

  1. Get the ABRecords id by ABRecordGetRecordID(). The ABRecordID is defined as int32_t so you can cast it to an NSInteger and store it wherever you like. You can later get the record back from ABAddressBookGetPersonWithRecordID() or ABAddressBookGetGroupWithRecordID(). 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屋!

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