开发者

how to store NSMutableArray in NSUserDefaults

开发者 https://www.devze.com 2023-02-04 09:05 出处:网络
Please tell me how can i storeNSMutableArray into NSUserDefaults. I have an array called data(NSArray).I want to add this array in NSMutableArray called mArray.

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 NSUserDe开发者_C百科faults. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号