2014-07-26 64 views
-2

我試圖解析與下面的代碼一個JSON對象:獲得「無法識別的選擇發送到實例」

NSDictionary *res = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingMutableLeaves error:&myError]; 

NSArray *test = [res allValues]; 
@NSLog(@"%@", test); 

我不斷收到這個錯誤,雖然我知道的值大致如下:

2014-07-25 22:31:17.652 Application[18009:60b] (
    { 
    "at_tag" = sam; 
    category = 1; 
    distance = "95.60"; 
    "hash_tag" = idk; 
    uid = 1; 
    vidURI = "http://server/userPosts/avfgt123.mp4"; 
}, 
    { 
    "at_tag" = nick; 
    category = 2; 
    distance = "99.45"; 
    "hash_tag" = irdk; 
    uid = 2; 
    vidURI = "http://server/userPosts/avfg2223.mp4"; 
} 
) 

整個錯誤我得到的是這樣的:

2014-07-25 22:33:13.829 App[18025:60b] -[__NSCFArray allValues]: unrecognized selector sent to instance 0x10d90c0d0 
2014-07-25 22:33:13.831 App[18025:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]: unrecognized selector sent to instance 0x10d90c0d0' 
*** First throw call stack: 
(
0 CoreFoundation      0x0000000102477495 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x00000001021d699e objc_exception_throw + 43 
2 CoreFoundation      0x000000010250865d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
3 CoreFoundation      0x0000000102468d8d ___forwarding___ + 973 
4 CoreFoundation      0x0000000102468938 _CF_forwarding_prep_0 + 120 
5 App        0x00000001000028a7 -[BFTMainViewController connectionDidFinishLoading:] + 263 
6 Foundation       0x0000000101ec736b __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 48 
7 Foundation       0x0000000101d7abdb -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 210 
8 Foundation       0x0000000101d7aaec -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 69 
9 CFNetwork       0x0000000103289637 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107 
10 CFNetwork       0x0000000103287802 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 84 
11 CoreFoundation      0x000000010241df74 CFArrayApplyFunction + 68 
12 CFNetwork       0x00000001031fa3e7 _ZN19RunloopBlockContext7performEv + 133 
13 CFNetwork       0x00000001031fa217 _ZN17MultiplexerSource7performEv + 247 
14 CFNetwork       0x00000001031fa03a _ZN17MultiplexerSource8_performEPv + 72 
15 CoreFoundation      0x0000000102406d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
16 CoreFoundation      0x00000001024065f2 __CFRunLoopDoSources0 + 242 
17 CoreFoundation      0x000000010242246f __CFRunLoopRun + 767 
18 CoreFoundation      0x0000000102421d83 CFRunLoopRunSpecific + 467 
19 GraphicsServices     0x0000000103cf2f04 GSEventRunModal + 161 
20 UIKit        0x0000000100d83e33 UIApplicationMain + 1010 
21 App        0x0000000100001693 main + 115 
22 libdyld.dylib      0x00000001039745fd start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我收到線的錯誤:

NSArray *test = [res allValues]; 

這難倒我現在很長一段時間,我覺得這是一些簡單的我從來沒有碰到了這個問題之前,不能把我的手指上。

+1

你的問題說明你的JSON對象實際上是NSDictionaries'的''一個NSArray',所以'allValues'將無法工作,因爲根對象不是'NSDictionary' 。又名:''NSArray'沒有可見的@interface'聲明選擇器'allValues' – klcjr89

回答

3

正如我上面的意見建議:你的問題說明你的JSON對象實際上是NSDictionariesNSArray(我認爲基於控制檯輸出,如果我錯了,讓我知道雖然),所以allValues方法將不工作,因爲根對象不是NSDictionary。又名:No visible @interface for NSArray declares the selector allValues試試這個:

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingMutableLeaves error:&myError]; 

NSMutableArray *mutableArray = [[NSMutableArray alloc]init]; 

for (NSDictionary *sub in jsonArray) 
{ 
    [mutableArray addObjectsFromArray:[sub allValues]]; 
} 

NSLog(@"Array is: %@",mutableArray); 
相關問題