OGeek|极客世界-中国程序员成长平台

标题: ios - 在 NSUserDefaults 中存储 NSMutableDictionary 时应用程序崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:48
标题: ios - 在 NSUserDefaults 中存储 NSMutableDictionary 时应用程序崩溃

“离开逻辑”有效,但我在将一个 NSMutableDictionary 保存到 NSUserDefaults 时遇到问题。

以下代码:

@implementation LoginService
{
  NSUserDefaults *prefs;
}

- (void) parseResponseNSDictionary*) dictionary
{
  NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithDictionary:dictionary];
  prefs = [NSUserDefaults standardUserDefaults];
  [prefs setObject:dic forKey“setKey”];
  [prefs synchronize];
 }

给我以下崩溃:

作为键 setKey 的 NSUserDefaults/CFPreferences 值

*** 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 +[NSException raise:format:] + 205
    3   CoreFoundation                      0x0000000110e26ef5 _CFPrefsValidateValueForKey + 149
    4   CoreFoundation                      0x0000000110e69a49 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 217
    5   CoreFoundation                      0x0000000110d9e5b7 -[CFPrefsPlistSource alreadylocked_setValue:forKey:] + 311
    6   CoreFoundation                      0x0000000110d9e44e -[CFPrefsSource setValue:forKey:] + 62
    7   CoreFoundation                      0x0000000110d5b991 +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] + 1105
    8   CoreFoundation                      0x0000000110d9e3b2 _CFPreferencesSetValueWithContainer + 306
    9   Foundation                          0x0000000110244b25 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 46
    10  test 2015                           0x000000010b5f2051 -[LoginService parseResponse:] + 5025
    11  test 2015                           0x000000010b567348 -[WebService successResponse:] + 104
    12  test 2015                           0x000000010b56693f __22-[WebService GETstart]_block_invoke + 127
    13  test 2015                           0x000000010b495d0b __74+[AFJSONRequestOperation JSONRequestOperationWithRequest:success:failure:]_block_invoke + 203
    14  test 2015                           0x000000010b4970d3 __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke78 + 51
    15  libdispatch.dylib                   0x00000001113d1e5d _dispatch_call_block_and_release + 12
    16  libdispatch.dylib                   0x00000001113f249b _dispatch_client_callout + 8
    17  libdispatch.dylib                   0x00000001113da2af _dispatch_main_queue_callback_4CF + 1738
    18  CoreFoundation                      0x0000000110dc6d09 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    19  CoreFoundation                      0x0000000110d882c9 __CFRunLoopRun + 2073
    20  CoreFoundation                      0x0000000110d87828 CFRunLoopRunSpecific + 488
    21  GraphicsServices                    0x000000011275dad2 GSEventRunModal + 161
    22  UIKit                               0x000000010ecfb610 UIApplicationMain + 171
    23  test 2015                           0x000000010b44fdbf main + 111
    24  libdyld.dylib                       0x000000011142692d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 



Best Answer-推荐答案


来自 Apple 的 Documentation value 参数只能是属性列表对象:NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。对于 NSArray 和 NSDictionary 对象,它们的内容必须是属性列表对象。请参阅什么是属性列表?在 Property List Programming Guide .

确保字典中的所有内容都是属性列表对象,简单的整数不起作用,因为所有内容都必须是对象。

所以,首先检查字典中的所有值,将任何整数/ bool 值转换为 NSNumbers,然后将其保存回字典,然后保存到 NSUserDefaults。

我可以从您的错误中看到字典包含整数,这些可能是导致问题的原因。

result =     {
        email = "";
        group = 1; //This Line here
        locations = "";
        "login_id" = "";
        photo = "";
        status = 1; //This line here
    };

确保上面的那些行是 NSNumber 对象。

关于ios - 在 NSUserDefaults 中存储 NSMutableDictionary 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343870/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4