同步NSUserDefaults时崩溃

时间:2014-10-21 19:46:19

标签: ios crash nsuserdefaults

当我尝试保存NSUserDefaults时,我的应用程序中出现了随机错误。任何帮助/建议将非常感谢!!!我已经四处搜索但没找到任何解决方案:|

在保存NSuserDefaults之前,我设置了3个值:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kInitialized];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kIsOK];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kInitDate]; 
[[NSUserDefaults standardUserDefaults] synchronize];

以下是崩溃日志:

Thread 11 Crashed:
0   libsystem_kernel.dylib          0x383361f0 __pthread_kill + 8
1   libsystem_pthread.dylib         0x383a0792 pthread_kill + 54
2   libsystem_c.dylib               0x382e7032 __abort + 74
3   libsystem_c.dylib               0x382e6fe4 abort + 84
4   libsystem_malloc.dylib          0x383656f4 szone_error + 312
5   libsystem_malloc.dylib          0x38365974 free_list_checksum_botch + 24
6   libsystem_malloc.dylib          0x3835fe02 small_malloc_from_free_list + 190
7   libsystem_malloc.dylib          0x3835cefa szone_malloc_should_clear + 1394
8   libsystem_malloc.dylib          0x3835c952 malloc_zone_malloc + 70
9   CoreFoundation                  0x2d51026c __CFBasicHashRehash + 324
10  CoreFoundation                  0x2d5116e4 __CFBasicHashAddValue + 96
11  CoreFoundation                  0x2d42bff0 CFBasicHashAddValue + 3308
12  CoreFoundation                  0x2d42c696 CFDictionaryAddValue + 178
13  CoreFoundation                  0x2d440c1c _flattenPlist + 164
14  CoreFoundation                  0x2d440ca2 _flattenPlist + 298
15  CoreFoundation                  0x2d4a48da __CFBinaryPlistWrite + 126
16  CoreFoundation                  0x2d440a48 CFPropertyListWrite + 240
17  CoreFoundation                  0x2d450dd4 CFPropertyListWriteToStream + 140
18  CoreFoundation                  0x2d44ea5c _CFXPreferencesWritePlist + 264
19  CoreFoundation                  0x2d44e94c -[CFXPreferencesPropertyListSourceSynchronizer writePlistToDisk] + 124
20  CoreFoundation                  0x2d44c1aa -[CFXPreferencesPropertyListSourceSynchronizer synchronizeAlreadyFlocked] + 482
21  CoreFoundation                  0x2d44bfc0 -[CFXPreferencesPropertyListSourceSynchronizer synchronize] + 16
22  CoreFoundation                  0x2d45d25e -[CFXPreferencesPropertyListSource synchronize] + 106
23  CoreFoundation                  0x2d45da9a -[CFXPreferencesSearchListSource synchronize] + 70
24  CoreFoundation                  0x2d4eb7ba ___CFXPreferencesAppSynchronize_block_invoke + 26
25  CoreFoundation                  0x2d4ec122 ____CFXPREFS_SYNC_LOCKED_block_invoke + 30
26  libdispatch.dylib               0x3825b81c _dispatch_client_callout + 20
27  libdispatch.dylib               0x382617c6 _dispatch_barrier_sync_f_invoke + 22
28  CoreFoundation                  0x2d4668b8 CFPreferencesAppSynchronize + 260
29  Foundation                      0x2de516ec -[NSUserDefaults(NSUserDefaults) synchronize] + 20

更新: 我已将setValue替换为setObject并继续观察我的应用程序。如果有没有这个更正,我会尽快向你汇报。

2 个答案:

答案 0 :(得分:2)

对于NSUserDefaults,您应该使用setObject:forKey:,而不是setValue:forKey:。这可能是导致崩溃的原因,但即使它没有导致崩溃,也是你想要改变的东西。

答案 1 :(得分:1)

这样做。 您正在创建NSDate的对象

 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kInitialized];
 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kIsOK];
 [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kInitDate];
 [[NSUserDefaults standardUserDefaults] synchronize];