保存自定义对象的数组 save and restore an array of custom objects
2014-11-02 17:54
786 查看
参考如下:
http://stackoverflow.com/questions/3648558/save-and-restore-an-array-of-custom-objects
http://www.crifan.com/store_save_array_of_custom_object_class_into_nsuserdefaults_in_iphone_ios/comment-page-1/
http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults
使用NSUserDefaults
归档array,使用如下方法:
[[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];
}
}
自定义的对象必须实现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;
}
http://stackoverflow.com/questions/3648558/save-and-restore-an-array-of-custom-objects
http://www.crifan.com/store_save_array_of_custom_object_class_into_nsuserdefaults_in_iphone_ios/comment-page-1/
http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults
使用NSUserDefaults
归档array,使用如下方法:
[[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];
}
}
自定义的对象必须实现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;
}
相关文章推荐
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- 【已解决】iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- 对象数组(Array of Objects)
- Given an array of positive and negative integers find the first subarray with zero sum
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- iOS开发——保存自定义对象数组、字典到文件
- Filter (seach and replace) array of bytes in an InputStream
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- How to sort an NSMutableArray with custom objects in it?
- pointer to ref of an array, and to the func that return a ref of an array
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- If you had an array of 1 million values and you had to remove duplicates