保存和恢复自定义对象数组

时间:2021-08-01 17:01:12

I have an NSArray of custom objects that I want to save and restore. Can this be done with NSUserDefaults?

我有一个自定义对象的NSArray,我想保存和恢复。这可以通过NSUserDefaults完成吗?

3 个解决方案

#1


26  

You can still use NSUserDefaults if you archive your array into NSData.

如果将阵列存档到NSData中,仍可以使用NSUserDefaults。

For Archiving your array, you can use the following code:

对于存档阵列,您可以使用以下代码:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"mySavedArray"];

And then for loading the custom objects in the array you can use this code:

然后,为了在数组中加载自定义对象,您可以使用以下代码:

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:@"mySavedArray"];
if (savedArray != nil)
{
        NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
        if (oldArray != nil) {
                customObjectArray = [[NSMutableArray alloc] initWithArray:oldArray];
        } else {
                customObjectArray = [[NSMutableArray alloc] init];
        }
}

Make sure you check that the data returned from the user defaults is not nil, because that may crash your app.

确保检查用户默认值返回的数据不是nil,因为这可能会导致应用程序崩溃。

The other thing you will need to do is to make your custom object comply to the NSCoder protocol. You could do this using the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.

您需要做的另一件事是使您的自定义对象符合NSCoder协议。你可以使用 - (void)encodeWithCoder:(NSCoder *)编码器和 - (id)initWithCoder:(NSCoder *)编码器方法。


EDIT.

Here's an example of what you might put in the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.

以下是您可能在 - (void)encodeWithCoder:(NSCoder *)编码器和 - (id)initWithCoder:(NSCoder *)编码器方法中放置的示例。

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:aLabel forKey:@"label"];
    [coder encodeInteger:aNumberID forKey:@"numberID"];
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [[CustomObject alloc] init];
    if (self != nil)
    {
        aLabel = [coder decodeObjectForKey:@"label"];
        aNumberID = [coder decodeIntegerForKey:@"numberID"];
    }   
    return self;
}

#2


7  

NSUserDefaults cannot write custom objects to file, only ones it knows about (NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate). Instead, you should take a look at the Archives and Serializations Programming Guide, as well as the NSCoding Protocol Reference, if you're looking to save and restore custom objects to disk. Implementing the protocol is not terribly difficult, and requires very little work.

NSUserDefaults无法将自定义对象写入文件,只能知道它所知道的对象(NSArray,NSDictionary,NSString,NSData,NSNumber和NSDate)。相反,如果您希望将自定义对象保存并还原到磁盘,则应该查看“存档和序列化编程指南”以及NSCoding协议参考。实施协议并不是非常困难,只需要很少的工作。

#3


1  

Custom objects, no. NSUserDefaults only knows about a few basic types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).

自定义对象,没有。 NSUserDefaults只知道一些基本类型(NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary)。

Could you use JSON (http://code.google.com/p/json-framework/) to convert your custom object to a string representation, then save an array of those to Defaults? (Using the setObject:forKey: method).

您可以使用JSON(http://code.google.com/p/json-framework/)将自定义对象转换为字符串表示形式,然后将其中的数组保存到默认值中吗? (使用setObject:forKey:方法)。

Otherwise, you could look at using sqlite, NSCoder, or even resort to fopen.

否则,你可以看看使用sqlite,NSCoder,甚至诉诸fopen。

#1


26  

You can still use NSUserDefaults if you archive your array into NSData.

如果将阵列存档到NSData中,仍可以使用NSUserDefaults。

For Archiving your array, you can use the following code:

对于存档阵列,您可以使用以下代码:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@"mySavedArray"];

And then for loading the custom objects in the array you can use this code:

然后,为了在数组中加载自定义对象,您可以使用以下代码:

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *savedArray = [currentDefaults objectForKey:@"mySavedArray"];
if (savedArray != nil)
{
        NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
        if (oldArray != nil) {
                customObjectArray = [[NSMutableArray alloc] initWithArray:oldArray];
        } else {
                customObjectArray = [[NSMutableArray alloc] init];
        }
}

Make sure you check that the data returned from the user defaults is not nil, because that may crash your app.

确保检查用户默认值返回的数据不是nil,因为这可能会导致应用程序崩溃。

The other thing you will need to do is to make your custom object comply to the NSCoder protocol. You could do this using the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.

您需要做的另一件事是使您的自定义对象符合NSCoder协议。你可以使用 - (void)encodeWithCoder:(NSCoder *)编码器和 - (id)initWithCoder:(NSCoder *)编码器方法。


EDIT.

Here's an example of what you might put in the -(void)encodeWithCoder:(NSCoder *)coder and -(id)initWithCoder:(NSCoder *)coder methods.

以下是您可能在 - (void)encodeWithCoder:(NSCoder *)编码器和 - (id)initWithCoder:(NSCoder *)编码器方法中放置的示例。

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:aLabel forKey:@"label"];
    [coder encodeInteger:aNumberID forKey:@"numberID"];
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [[CustomObject alloc] init];
    if (self != nil)
    {
        aLabel = [coder decodeObjectForKey:@"label"];
        aNumberID = [coder decodeIntegerForKey:@"numberID"];
    }   
    return self;
}

#2


7  

NSUserDefaults cannot write custom objects to file, only ones it knows about (NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate). Instead, you should take a look at the Archives and Serializations Programming Guide, as well as the NSCoding Protocol Reference, if you're looking to save and restore custom objects to disk. Implementing the protocol is not terribly difficult, and requires very little work.

NSUserDefaults无法将自定义对象写入文件,只能知道它所知道的对象(NSArray,NSDictionary,NSString,NSData,NSNumber和NSDate)。相反,如果您希望将自定义对象保存并还原到磁盘,则应该查看“存档和序列化编程指南”以及NSCoding协议参考。实施协议并不是非常困难,只需要很少的工作。

#3


1  

Custom objects, no. NSUserDefaults only knows about a few basic types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).

自定义对象,没有。 NSUserDefaults只知道一些基本类型(NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary)。

Could you use JSON (http://code.google.com/p/json-framework/) to convert your custom object to a string representation, then save an array of those to Defaults? (Using the setObject:forKey: method).

您可以使用JSON(http://code.google.com/p/json-framework/)将自定义对象转换为字符串表示形式,然后将其中的数组保存到默认值中吗? (使用setObject:forKey:方法)。

Otherwise, you could look at using sqlite, NSCoder, or even resort to fopen.

否则,你可以看看使用sqlite,NSCoder,甚至诉诸fopen。