今天在代码测试中发现以下代码:
// 更新本地缓存
NSMutableDictionary *gesture = (NSMutableDictionary *)[[NSUserDefaults standardUserDefaults] objectForKey:Lock_Gesture_Dic];
[gesture setValue:self.access_token forKey:Lock_Access_Token];
执行后错误信息如下:
2016-05-06 15:12:18.566[5054:105571] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f312d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010ed86deb objc_exception_throw + 48
2 CoreFoundation 0x000000010f312cbd +[NSException raise:format:] + 205
3 CoreFoundation 0x000000010f239133 -[__NSCFDictionary setObject:forKey:] + 99
4 Proj 0x0000000109ef641b +[AppDelegate aaaaa] + 3083
其实这是不被推荐的,官方建议写法:
NSMutableDictionary *gesture = [(NSMutableDictionary*) [[NSUserDefaults standardUserDefaults] objectForKey:Lock_Gesture_Dic] mutableCopy];