Shishir.bobby
Shishir.bobby

Reputation: 10984

retain array into NSDefault?

HI all,

can we retain an array in NSDefualt?

regards shishir

Upvotes: 0

Views: 906

Answers (2)

vodkhang
vodkhang

Reputation: 18741

I guess that you are asking for:

You have an array, you put it into NSUserDefaults, then later, you want to retain that array, is it right?

NSUserDefaults will not retain your array when you put it into. It just save and get data through serialization to a database So, if you currently hold your array, you don't need to retain it any more.

Upvotes: 0

Jason Coco
Jason Coco

Reputation: 78393

If you mean store an array, then the answer is yes. You just use the setObject:forKey: method to store it. So:

NSArray* myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArrayKey"];

// much later...

NSArray* thatArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"myArrayKey];
NSLog(@"thatArray second object: %@", [thatArray objectAtIndex:1]);
// prints "thatArray second object: two"

Upvotes: 2

Related Questions