如何将自定义对象保存在数组中并将其存储在

如何将自定义对象保存在数组中并将其存储在

本文介绍了如何将自定义对象保存在数组中并将其存储在 NSUserDefaults 中 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按钮操作方法之后获取我的自定义类对象.现在我需要在 NSMutableArray 中存储多个自定义对象,然后在 NSUserDefaults 中存储这个数组.

I am getting my custom class object after button action method. Now I need to store multiple custom objects in NSMutableArray and then store this array in NSUserDefaults.

这是我的代码:

-(IBAction)onClickSubmitLater:(id)sender
{
    //Saving store in user defaults for later upload data.

    NSMutableArray *arrayStoreList = [[NSMutableArray alloc] init];
    arrayStoreList = [Util getArrayPreference:@"Store"];//arrayStoreList is the list of all stores.

    Store *store = [[Store alloc] init];
    store = [arrayStoreList objectAtIndex:self.selectedStoreIndex];//here i am getting particular store that i need to save in array.

    //archive
    NSData *dataStore = [NSKeyedArchiver archivedDataWithRootObject:store];
    [[NSUserDefaults standardUserDefaults] setObject:dataStore forKey:@"resultStore"];

    //unarchive
    NSData *dataResultStore = [[NSUserDefaults standardUserDefaults] objectForKey:@"resultStore"];

    Store *resultStore = (Store *)[NSKeyedUnarchiver unarchiveObjectWithData:dataResultStore];
    NSLog(@"%@", resultStore);
}

使用上面的代码,它在NSUserDefaults中保存了单个自定义类Store对象.所以我想在提交以后的ibaction之后将所有Store对象保存在NSMutableArray中.稍后,我将一一获取商店数组以将商店上传到服务器上.

Using above code, it is saving single Custom class Store object in NSUserDefaults. So I would like to save all Store object in NSMutableArray after submit later ibaction. Later, I will fetch array of stores for uploading store on server one by one.

谢谢.

推荐答案

要存储和检索具有用户默认值的自定义对象的数组,您可以使用以下方法:

To store and retrieve array with custom object on user defaults you can use following methods:

-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
    [defaults setObject:data forKey:keyName];
    [defaults synchronize];
}

-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:keyName];
    NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    [defaults synchronize];
    return myArray;
}

但请确保您已实施

- (void) encodeWithCoder : (NSCoder *)encode ;
- (id) initWithCoder : (NSCoder *)decode;

数据模型类中避免崩溃的方法如下:

method in data model class to avoid crash as followings:

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

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

这篇关于如何将自定义对象保存在数组中并将其存储在 NSUserDefaults 中 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:19