ios - 在 NSUserDefaults 中存储 NSMutableDictionary 时应用程序崩溃
<p><p>“离开逻辑”有效,但我在将一个 NSMutableDictionary 保存到 NSUserDefaults 时遇到问题。</p>
<p>以下代码:</p>
<pre><code>@implementation LoginService
{
NSUserDefaults *prefs;
}
- (void) parseResponse:(NSDictionary*) dictionary
{
NSMutableDictionary *dic=[initWithDictionary:dictionary];
prefs = ;
;
;
}
</code></pre>
<p>给我以下崩溃:</p>
<p>作为键 setKey 的 NSUserDefaults/CFPreferences 值</p>
<pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
result = {
email = "";
group = 1;
locations = "";
"login_id" = "";
photo = "";
status = 1;
};
} for key setKey'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110e66e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001108b6deb objc_exception_throw + 48
2 CoreFoundation 0x0000000110e66d9d + + 205
3 CoreFoundation 0x0000000110e26ef5 _CFPrefsValidateValueForKey + 149
4 CoreFoundation 0x0000000110e69a49 - + 217
5 CoreFoundation 0x0000000110d9e5b7 - + 311
6 CoreFoundation 0x0000000110d9e44e - + 62
7 CoreFoundation 0x0000000110d5b991 + + 1105
8 CoreFoundation 0x0000000110d9e3b2 _CFPreferencesSetValueWithContainer + 306
9 Foundation 0x0000000110244b25 - + 46
10test 2015 0x000000010b5f2051 - + 5025
11test 2015 0x000000010b567348 - + 104
12test 2015 0x000000010b56693f __22-_block_invoke + 127
13test 2015 0x000000010b495d0b __74+_block_invoke + 203
14test 2015 0x000000010b4970d3 __64-_block_invoke78 + 51
15libdispatch.dylib 0x00000001113d1e5d _dispatch_call_block_and_release + 12
16libdispatch.dylib 0x00000001113f249b _dispatch_client_callout + 8
17libdispatch.dylib 0x00000001113da2af _dispatch_main_queue_callback_4CF + 1738
18CoreFoundation 0x0000000110dc6d09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
19CoreFoundation 0x0000000110d882c9 __CFRunLoopRun + 2073
20CoreFoundation 0x0000000110d87828 CFRunLoopRunSpecific + 488
21GraphicsServices 0x000000011275dad2 GSEventRunModal + 161
22UIKit 0x000000010ecfb610 UIApplicationMain + 171
23test 2015 0x000000010b44fdbf main + 111
24libdyld.dylib 0x000000011142692d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>来自 Apple 的 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/setObject:forKey:" rel="noreferrer noopener nofollow">Documentation</a>
value 参数只能是属性列表对象:NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。对于 NSArray 和 NSDictionary 对象,<strong>它们的内容必须是属性列表对象</strong>。请参阅什么是属性列表?在 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html#//apple_ref/doc/uid/10000048i-CH3-54303" rel="noreferrer noopener nofollow">Property List Programming Guide</a> .</p>
<p>确保字典中的所有内容都是属性列表对象,简单的整数不起作用,因为所有内容都必须是对象。 </p>
<p>所以,首先检查字典中的所有值,将任何整数/ bool 值转换为 NSNumbers,然后将其保存回字典,然后保存到 NSUserDefaults。</p>
<p>我可以从您的错误中看到字典包含整数,这些可能是导致问题的原因。</p>
<pre><code>result = {
email = "";
group = 1; //This Line here
locations = "";
"login_id" = "";
photo = "";
status = 1; //This line here
};
</code></pre>
<p>确保上面的那些行是 NSNumber 对象。</p></p>
<p style="font-size: 20px;">关于ios - 在 NSUserDefaults 中存储 NSMutableDictionary 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36343870/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36343870/
</a>
</p>
页:
[1]