Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store NSMutableArray in NSUserDefaults

Please tell me how can i store NSMutableArray into NSUserDefaults.

I have an array called data(NSArray).I want to add this array in NSMutableArray called mArray.

[mArray addObject:data];
[prefs setObject:mArray forkey:@"test"];
[prefs synchronize];

& want to store mArray values in NSUserDefaults. Please tell me how can i store & retrive these values. above code is storing only last value not appending value in NSUserDefaults.

Thanks

like image 975
user440485 Avatar asked Jan 14 '11 10:01

user440485


1 Answers

You can't. See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html, in particular :

Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey: will be immutable.

Instead, make a mutableCopy of the array you retrieve from NSUserDefaults, add your object, then set your new array back in.

like image 73
Jonathan del Strother Avatar answered Sep 18 '22 19:09

Jonathan del Strother