简体   繁体   English

NSMutableArray中的读取元素

[英]Reading Element in NSMutableArray

I have created an NSMutableArray based on the JSON Elements in a Twitter Tweet. 我已经在Twitter Tweet中基于JSON元素创建了NSMutableArray。 However when attempting to read an element from the array I get this error: 但是,当尝试从数组中读取元素时,出现此错误:

 -[__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

Here is my code in how I am attempting to read the NSMutableArray: 这是我尝试读取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);
}

As the error says. 正如错误所说。

[__NSCFArray objectForKey:]

You are treating NSArray as NSDictionary. 您正在将NSArray视为NSDictionary。 Because objectForKey: applies on NSDictionary. 因为objectForKey:适用于NSDictionary。

And also from your code if you realize this 而且如果您意识到这一点,也可以从您的代码中

Code No:1 编号:1

NSMutableArray *hashtag = [[tweet objectForKey:@"entities"] objectForKey:@"hashtags"];

This is an array of hashtags and in second line you have 这是一个主题标签数组,在第二行中

Code No:2 编号:2

 NSString *hashtag = tweet[@"entities"][@"hashtags"][@"text"];

If you rethink tweet[@"entities"][@"hashtags"] this returns an array hashtag of hashtags same of your above Code No:1. 如果您重新考虑tweet[@"entities"][@"hashtags"]则会返回与上面的代码编号1相同的hashtag数组hashtag And from array you are trying to fetch object in key text which is the exact issue. 从数组中您正在尝试获取关键text中的对象,这正是确切的问题。

You should go to for loop to get each text from hastags array 您应该转到for循环以从hastags数组获取每个文本

for(NSString *strhastag in hashtag){
  NSLog(@"your strhastag = %@",strhastag); // This will return you all hastags
}

Array and contains Array of Dictionary so if you perform operation objectForKey on NSArray will crash with error message "Unrecognized selector" 数组并包含字典数组,因此,如果在NSArray上执行objectForKey操作,则会崩溃,并显示错误消息“无法识别的选择器”

check this [__NSCFArray objectForKey:]: unrecognized selector sent to instance 检查此[__NSCFArray objectForKey:]:无法识别的选择器已发送到实例

This might helps you :) 这可能会帮助您:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM