NSUserDefaults在同步时崩溃

时间:2012-07-26 17:58:38

标签: ios cocoa nsuserdefaults

当我的应用程序加载时,我在NSUserDedaults字典上执行一些操作。 对于某些用户,应用程序崩溃的原因如下:

NSUserDefaults* prefs = [NSUserDefaults standartUserDefaults];
NSString* nsKey = [NSString stringWithUTF8String:ket.c_str()];

[prefs removeObjectForKey:nsKey];
[prefs synchronize];

崩溃发生在最后一行(同步)。 我无法弄清楚为什么,这对我来说不会发生。

有什么想法吗?

EDIT ::

This is the crash:

0   CoreFoundation                      0x34d02e58 CFRetain + 20
1   CoreFoundation                      0x34d0a9bf _CFArrayReplaceValues + 235
2   CoreFoundation                      0x34d0f0e5 CFArrayAppendValue + 125
3   CoreFoundation                      0x34d26d57 _flattenPlist + 139
4   CoreFoundation                      0x34d26dbd _flattenPlist + 241
5   CoreFoundation                      0x34d6e08f __CFBinaryPlistWrite + 127
6   CoreFoundation                      0x34d26cad CFPropertyListWrite + 429
7   CoreFoundation                      0x34d26aa5 CFPropertyListWriteToStream + 221
8   CoreFoundation                      0x34d25fc1 _CFXPreferencesWritePlist + 273
9   CoreFoundation                      0x34d25ead -[CFXPreferencesPropertyListSourceSynchronizer writePlistToDisk] + 129
10  CoreFoundation                      0x34d22699 -[CFXPreferencesPropertyListSourceSynchronizer synchronizeAlreadyFlocked] + 601
11  CoreFoundation                      0x34d2243d -[CFXPreferencesPropertyListSourceSynchronizer synchronize] + 21
12  CoreFoundation                      0x34d31b3b -[CFXPreferencesPropertyListSource synchronize] + 111
13  CoreFoundation                      0x34d32b93 -[CFXPreferencesSearchListSource synchronize] + 75
14  CoreFoundation                      0x3719d87f CFArrayRemoveAllValues + 122
15  CoreFoundation                      0x3719e1f1 CFRunLoopTimerIsValid + 36
16  libdispatch.dylib                   0x3435c7e7 __func__.16042 + 2384
17  libdispatch.dylib                   0x3435c647 __func__.16042 + 1968
18  libdispatch.dylib                   0x3435c267 __func__.16042 + 976
19  libdispatch.dylib                   0x3435c911 __func__.16042 + 2682
20  CoreFoundation                      0x34d39c67 CFPreferencesAppSynchronize + 335
21  Foundation                          0x32c4c8ef -[ABMultiCellContentView_Address tagForPropertyKey:] + 30

1 个答案:

答案 0 :(得分:2)

试试这样:

NSString* nsKey = [NSString stringWithUTF8String:ket.c_str()];
if([[NSUserDefaults standardUserDefaults] stringForKey:nsKey] != nil)
{
    [[NSUserDefaults standartUserDefaults] removeObjectForKey:nsKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}