码迷,mamicode.com
首页 > 编程语言 > 详细

NSUserDefaults 存储可变数组问题

时间:2015-10-14 23:22:17      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

废话不多说上代码,项目中遇到的问题是:

NSMutableArray * arrayLoginNames = [NSMutableArray array];

arrayLoginNames  = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:KLastLoginNameForAutoSendMsgToKeFu];

这句话取出了数组,可是当对数组添加元素后,进行存储时卡在了下面这句话:

[[NSUserDefaults standardUserDefaults] setObject:arrayLoginNames forKey:KLastLoginNameForAutoSendMsgToKeFu];

把主线程卡主,但是不崩溃,不知道为何,就Google了,stackoverflow给了解答办法,

When you store mutable objects to NSUserDefaults, it stores an immutable copy of it so you can‘t change it directly like that. You have to get the mutable copy out of defaults, change it, and then set it back, replacing old object in defaults.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *mutableArrayCopy = [[prefs objectForKey:@"theArray"] mutableCopy];
[mutableArrayCopy addObject:@"some new value"];
[prefs setObject:mutableArrayCopy forKey:@"theArray"];

[[NSUserDefaults standardUserDefaults] synchronize];

 

 

NSUserDefaults 存储可变数组问题

标签:

原文地址:http://www.cnblogs.com/daaiwusehng/p/4878922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!