废话不多说上代码,项目中遇到的问题是:
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];
时间: 2024-10-13 22:51:29