获取“无法识别的选择器发送到实例”

时间:2014-07-26 02:39:34

标签: ios objective-c json parsing unrecognized-selector

我正在尝试使用以下代码解析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 个答案:

答案 0 :(得分:3)

正如我上面的评论建议:你的问题显示你的JSON对象实际上是NSArray NSDictionaries(我认为基于控制台输出,如果我错了,请告诉我),因此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);