![](/img/trans.png)
[英]setContentViewController method in popover iOS8 causes app crash
[英]NSUserDefaults setObject method causes the app to crash in ios8
我有一個應用程序,其中我正在使用NSUserDefaults在viewController之間傳遞一些數據。
這是我正在使用的以下代碼。
[[NSUserDefaults standardUserDefaults]setObject:selectedLists forKey:UserID];
其中selectedLists是NSMutableArray,而UserID是NSString 。
直到ios7和xCode5,此代碼才能正常工作。
由於我已經更新了xCode6,現在使用的是ios8,因此我的應用程序可以在xCode 6和ios 7.1上正常運行,但是當我在ios8設備中運行該應用程序時,該應用程序崩潰了,並出現以下錯誤。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x7a027060'
我只是不明白這里出了什么問題。
如果有人遇到過ios8的此問題,請幫助我。
提前致謝。
selectedLists的值
{
CardFace = Word;
ImportFlag = Import;
ListID = 1;
ListName = "Adjectives - Appearance";
QuizBy = Definition;
UserID = 1;
}
並且UserId為1。
已編輯
*** First throw call stack:
(
0 CoreFoundation 0x06a24df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x03ac8a97 objc_exception_throw + 44
2 CoreFoundation 0x06a2ca75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x069759c7 ___forwarding___ + 1047
4 CoreFoundation 0x0697558e _CF_forwarding_prep_0 + 14
5 CoreFoundation 0x068df30f CFStringGetLength + 143
6 CoreFoundation 0x069de510 _CFPrefsEncodeKeyValuePairIntoMessage + 64
7 CoreFoundation 0x06a275cf -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 111
8 CoreFoundation 0x06954f3a -[CFPrefsPlistSource alreadylocked_setValue:forKey:] + 250
9 CoreFoundation 0x06954e22 -[CFPrefsSource setValue:forKey:] + 82
10 CoreFoundation 0x06954dc3 ___CFPreferencesSetValueWithContainer_block_invoke + 51
11 CoreFoundation 0x0690ef0a +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] + 1274
12 CoreFoundation 0x06954d61 _CFPreferencesSetValueWithContainer + 225
13 CoreFoundation 0x06a0aa62 _CFPreferencesSetAppValueWithContainer + 66
14 Foundation 0x0347f53f -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 59
15 Foundation 0x0353eeba -[NSUserDefaults(NSKeyValueCoding) setValue:forKey:] + 68
16 Vocab 0x000eaecc Vocab + 720588
17 libobjc.A.dylib 0x03ade7cd -[NSObject performSelector:withObject:withObject:] + 84
18 UIKit 0x022a079d -[UIApplication sendAction:to:from:forEvent:] + 99
19 UIKit 0x022a072f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
20 UIKit 0x023d3a16 -[UIControl sendAction:to:forEvent:] + 69
21 UIKit 0x023d3e33 -[UIControl _sendActionsForEvents:withEvent:] + 598
22 UIKit 0x023d309d -[UIControl touchesEnded:withEvent:] + 660
23 UIKit 0x022f0aba -[UIWindow _sendTouchesForEvent:] + 874
24 UIKit 0x022f1595 -[UIWindow sendEvent:] + 791
25 UIKit 0x022b6aa9 -[UIApplication sendEvent:] + 242
26 UIKit 0x022c68de _UIApplicationHandleEventFromQueueEvent + 20690
27 UIKit 0x0229b079 _UIApplicationHandleEventQueue + 2206
28 CoreFoundation 0x069487bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x0693e2cd __CFRunLoopDoSources0 + 253
30 CoreFoundation 0x0693d828 __CFRunLoopRun + 952
31 CoreFoundation 0x0693d1ab CFRunLoopRunSpecific + 443
32 CoreFoundation 0x0693cfdb CFRunLoopRunInMode + 123
33 GraphicsServices 0x0436424f GSEventRunModal + 192
34 GraphicsServices 0x0436408c GSEventRun + 104
35 UIKit 0x0229ee16 UIApplicationMain + 1526
36 Vocab 0x0004c9bc Vocab + 72124
37 libdyld.dylib 0x03ee6ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
在tableview的didselect方法上進行了編輯 ...
UserID = [[users objectAtIndex:indexPath.row] valueForKey:@"UserID"];
用戶是一個數組。
您應該檢查UserID類型,因為崩潰與將NSArray
更改為NSMutableArray
無關。
您必須在userID上將NSNumber與NSString混淆:
更改您的代碼,而不使用userID使用變量userIDString
NSString userIDString;
//If UserID is class of NSNumber turn its value to NSString
if ([UserID isKindOfClass:[NSNumber class]])
{
userIDString = [UserID stringValue];
}
else
userIDString = UserID ;
在運行時, UserId
的類型可能為NSNumber
,即使在您的代碼中它的類型為NSString
。
檢查如何創建UserId
。 使用調試器檢查運行時類型。
新發布的代碼顯示,您從users
數組中檢索了UserId
,其中可能包含字典。 在那些詞典中,鍵UserID
帶有一個值。 這取決於字典數組的創建方式(json反序列化?),但看來UserID
鍵所引用的對象類型是數字類型,而不是字符串。 因此,您要做的就是將此數字轉換為字符串:
UserID = [[[users objectAtIndex:indexPath.row] valueForKey:@"UserID"] stringValue];
現在,您的密鑰始終是字符串。
我認為自iOS8起,無法在NSUserDefaults
存儲NSMutableArrays
(根據不同的帖子)。 嘗試先將其轉換為數組(或創建一個新數組)。
例如這樣:
NSArray *arr = [NSArray arrayWithArray:selectedLists];
並將arr
存儲在NSUserDefaults
。
不應使用NSUSerdefault保留自定義對象的集合。 您應該使用NSkeyedUnarchiver / NSkeyedarchiver保存它們。 參考:NSCoding。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.