5

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

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
user440485
  • 787
  • 2
  • 11
  • 20

1 Answers1

21

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.

Jonathan del Strother
  • 2,552
  • 19
  • 32