Ios 获得;已将无法识别的选择器发送到实例";
我试图用以下代码解析JSON对象:Ios 获得;已将无法识别的选择器发送到实例";,ios,objective-c,json,parsing,unrecognized-selector,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.6
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];
这让我困惑了很长一段时间,我觉得这很简单,我以前从来没有遇到过这个问题,也无法对此加以解释。正如我上面的评论所建议的:你的问题表明你的JSON对象实际上是一个
NSArray
的NSDictionaries
(我认为基于控制台输出,如果我错了,请告诉我),因此allValues
方法将不起作用,因为根对象不是NSDictionary
。Aka:No visible@interface for NSArray声明选择器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);
您的问题表明,您的JSON对象实际上是
NSDictionary
的NSArray
,因此allValues
将不起作用,因为根对象不是NSDictionary
。Aka:No visible@interface for'NSArray'声明选择器“allValues”