2

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.

Here is my code :

-(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);
}

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.

Thanks.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • - (void) encodeWithCoder : (NSCoder *)encode ; - (id) initWithCoder : (NSCoder *)decode; i have implemented in Store entity. Why downvote ? – Anand Gautam Dec 17 '13 at 06:57
  • NSUserDefaults are used for storing preferences, so ask this question to yourself "Is it necessary to use NSUserDefaults"? There are many other option to persist data like storing as plist, using coder and decoder etc. There is a beautiful reusable class written by nick lockwood, Have a look at https://github.com/nicklockwood/BaseModel . I suggest you using it. Thanks! – iCoder Dec 17 '13 at 06:59
  • Yes, according to my requirement, i have to use userDefaults. After stop an app from Xcode. it will remains the same store. – Anand Gautam Dec 17 '13 at 07:05

3 Answers3

19

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; 
}

But make sure you have implemented

- (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;
}
Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
  • Thanks for helping me and also thanks for avoid crash. – Mihir Oza Sep 16 '15 at 14:45
  • @AbdullahMd.Zubair Do i have to use `encodeWithCoder` and `initWithCoder` anywhere in the code? – Anurag Sharma Jun 21 '17 at 12:27
  • you must have to implement, otherwise will not work! – Abdullah Md. Zubair Jun 21 '17 at 12:30
  • But you are not using anywhere! Actually i didn't get this – Anurag Sharma Jun 21 '17 at 12:35
  • You have to implement that for your model – Abdullah Md. Zubair Jun 21 '17 at 13:27
  • as example you have 2 properties names label & numberID, you have to implement those method like `- (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; }` otherwise you will get crash. – Abdullah Md. Zubair Jun 21 '17 at 13:29
2

You need to archive it by implementing NSCoding protocol. Then you can write it to any file.

Implement these methods in your model :

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

As you say, you have implemented this in the Class, now you need to save the array.

For saving and reading the array, you need to use :;

[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"keyForArray"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"keyForArray"]];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

This two methods will mainly used to store and retrieve custom object's property.

- (void)encodeWithCoder:(NSCoder *)encoder ;
- (id)initWithCoder:(NSCoder *)decoder;

Take look at @chrissr's answer, @Brad Larson's answer

Community
  • 1
  • 1
Mani
  • 17,549
  • 13
  • 79
  • 100
  • Thanks mani, i have implemented above NSCoding methods in store entity class. – Anand Gautam Dec 17 '13 at 06:55
  • If those comments helped you, give tick or upvotes for visitor's attention. :) – Mani Dec 17 '13 at 06:58
  • Correct @mani, but my problem is to save all Store object in NSMutableArray. using above code i am saving one class object in Userdefaults. like this first i need to add all store in an array and then store this array in userdefault. – Anand Gautam Dec 17 '13 at 07:01
  • Did you see @Brad Larson's answer which I've added link? – Mani Dec 17 '13 at 07:04
  • 2
    Dude You said that you have used his code to implement above method than you have to show respect and give him up vote.+1 –  Dec 30 '13 at 05:41