保存和恢复自定义对象数组 [英] save and restore an array of custom objects
问题描述
我有一个NSArray自定义对象,我想保存和恢复。是否可以使用NSUserDefaults?
您仍然可以使用 NSUserDefaults 如果将数组归档到 NSData
。
对于归档数组,您可以使用以下代码:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myArray] forKey:@mySavedArray];
然后,要在数组中加载自定义对象,可以使用以下代码:
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];
}
}
确保您检查从用户返回的数据默认值不是 nil
,因为这可能会使您的应用程序崩溃。
另一件你需要做的是使您的自定义对象符合 NSCoder
协议。你可以使用 - (void)encodeWithCoder:(NSCoder *)coder
和 - (id)initWithCoder:(NSCoder *)coder
方法。
>
这里是一个你可能放在 - (void)encodeWithCoder:(NSCoder *)coder
和 - (id)initWithCoder:(NSCoder * )coder
方法。
- (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;
}
I have an NSArray of custom objects that I want to save and restore. Can this be done with NSUserDefaults?
You can still use NSUserDefaults if you archive your array into NSData
.
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.
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.
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 *)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;
}
这篇关于保存和恢复自定义对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!