我已经在Twitter Tweet中基于JSON元素创建了一个NSMutableArray。但是,当尝试从数组中读取元素时,我收到此错误:
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fea49d31730
2015-03-09 00:30:53.492 Floadt[3963:325552] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fea49d31730'
*** First throw call stack:
(
0 CoreFoundation 0x00000001059c7f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105660bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001059cf04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010592727c ___forwarding___ + 988
4 CoreFoundation 0x0000000105926e18 _CF_forwarding_prep_0 + 120
5 Floadt 0x00000001022cf456 -[TweetDetailViewController detectEntitesAndOrganize] + 1462
6 Floadt 0x00000001022ccb75 -[TweetDetailViewController viewDidLoad] + 853
7 UIKit 0x00000001044cda90 -[UIViewController loadViewIfRequired] + 738
8 UIKit 0x00000001044cdc8e -[UIViewController view] + 27
9 UIKit 0x00000001044f1507 -[UINavigationController _startCustomTransition:] + 633
10 UIKit 0x00000001044fd3fe -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
11 UIKit 0x00000001044fdf47 -[UINavigationController __viewWillLayoutSubviews] + 43
12 UIKit 0x0000000104643509 -[UILayoutContainerView layoutSubviews] + 202
13 UIKit 0x0000000104421973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
14 QuartzCore 0x00000001041f4de8 -[CALayer layoutSublayers] + 150
15 QuartzCore 0x00000001041e9a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16 QuartzCore 0x00000001041e987e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
17 QuartzCore 0x000000010415763e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
18 QuartzCore 0x000000010415874a _ZN2CA11Transaction6commitEv + 390
19 QuartzCore 0x0000000104158db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
20 CoreFoundation 0x00000001058fcdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
21 CoreFoundation 0x00000001058fcd20 __CFRunLoopDoObservers + 368
22 CoreFoundation 0x00000001058f2b53 __CFRunLoopRun + 1123
23 CoreFoundation 0x00000001058f2486 CFRunLoopRunSpecific + 470
24 GraphicsServices 0x000000010734f9f0 GSEventRunModal + 161
25 UIKit 0x00000001043a8420 UIApplicationMain + 1282
26 Floadt 0x00000001022d30d3 main + 115
27 libdyld.dylib 0x0000000107b74145 start + 1
28 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
以下是我试图阅读NSMutableArray的代码:
NSDictionary *tweet = self.detailItem;
NSMutableArray *hashtag = [[tweet objectForKey:@"entities"] objectForKey:@"hashtags"];
NSLog(@"Number of Hashtags = %ld",hashtag.count);
self.numberOfHashtags = hashtag.count;
if (self.numberOfHashtags>0){
NSString *hashtag = tweet[@"entities"][@"hashtags"][@"text"];
NSLog(@"Hashtags: %@",hashtag);
}
NSMutableArray *url = [[tweet objectForKey:@"entities"] objectForKey:@"urls"];
NSLog(@"Number of URL's = %ld",url.count);
self.numberOfUrls = url.count;
if (self.numberOfUrls>0){
NSString *url = [[[tweet objectForKey:@"entities"] objectForKey:@"urls"] objectForKey:@"display_url"];
NSLog(@"URL: %@",url);
}
NSMutableArray *user_mention = [[tweet objectForKey:@"entities"] objectForKey:@"user_mentions"];
NSLog(@"Number of User Mentions = %ld",user_mention.count);
self.numberOfMentions = user_mention.count;
NSMutableArray *media = [[tweet objectForKey:@"entities"] objectForKey:@"media"];
NSLog(@"Number of Media = %ld",media.count);
self.numberOfMedia = media.count;
if (self.numberOfMedia>0){
NSString *media = [[[tweet objectForKey:@"entities"] objectForKey:@"media"] objectForKey:@"media_url"];
NSLog(@"Media URL: %@",media);
}
答案 0 :(得分:0)
正如错误所说。
[__NSCFArray objectForKey:]
您正在将NSArray视为NSDictionary。因为objectForKey:
适用于NSDictionary。
如果您意识到这一点,也可以从您的代码中获取
代码:1
NSMutableArray *hashtag = [[tweet objectForKey:@"entities"] objectForKey:@"hashtags"];
这是一个主题标签数组,在第二行你有
代码:2
NSString *hashtag = tweet[@"entities"][@"hashtags"][@"text"];
如果您重新考虑tweet[@"entities"][@"hashtags"]
,则会返回与上述代码编号1相同的数组的hashtag
数组。从数组中,您尝试在密钥text
中获取对象,这是确切的问题。
您应该转到for循环以获取hastags
数组
for(NSString *strhastag in hashtag){
NSLog(@"your strhastag = %@",strhastag); // This will return you all hastags
}
答案 1 :(得分:0)
数组并包含字典数组,因此如果您在objectForKey
上执行操作NSArray
将崩溃并显示错误消息“无法识别的选择器”
检查此[__NSCFArray objectForKey:]: unrecognized selector sent to instance
这可能对您有所帮助:)。