user440485
user440485

Reputation: 797

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

Upvotes: 5

Views: 6795

Answers (1)

Jonathan del Strother
Jonathan del Strother

Reputation: 2552

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.

Upvotes: 21

Related Questions