tags:

views:

146

answers:

2

HI all,

can we retain an array in NSDefualt?

regards shishir

+2  A: 

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"
Jason Coco
@jasonactually i m filling table view with an array,when i delete row5 from table, it should be deleted.but if use the above code,i am not able to delete the row at all.plz suggest me to move ahead.regards
shishir.bobby
@shishir.bobby: Can you post some of the code you are using? There is not enough information to help otherwise.
Jason Coco
A: 

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.

vodkhang
That is not true. Adding and removing objects from NSUserDefaults has not effect on the object's retain count at all. NSUserDefaults serializes copies of the object into a persistent store. It does not send retain or release messages to the objects that it serializes.
Jason Coco
Ah yeah, I got it now. It was my misunderstanding. Previously, I just thought that we have to set object and then later we have to explicitly call write to file. But it is not correct. Thanks a lot
vodkhang
actually array is already retain .i dont want to retain that array after deleting some elements from array.....
shishir.bobby