NSUserDefaults在同步时崩溃

4

当我的应用程序正在加载时,我对NSUserDedaults字典执行一些操作。 对于某些用户,应用程序在以下逻辑中崩溃:

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

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

程序崩溃发生在最后一行(同步)。

我无法弄清楚原因,而且我自己没有遇到过。

有任何想法吗?

编辑:

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

当您调用其上的remove方法时,您确定nskey已经存在于用户默认设置中吗? - Mick MacCallum
是的 - 我执行 allKeys 来获取所有键并遍历它们。 - Erik Sapir
你在初始化nsKey之前检查了ket是否为空吗? - mkral
是的,我确定该字符串不为空。 - Erik Sapir
1个回答

2

只需像这样尝试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接