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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With