简体   繁体   中英

Reading Element in NSMutableArray

I have created an NSMutableArray based on the JSON Elements in a Twitter Tweet. 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:

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. Because objectForKey: applies on NSDictionary.

And also from your code if you realize this

Code No:1

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

This is an array of hashtags and in second line you have

Code No: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. And from array you are trying to fetch object in key text which is the exact issue.

You should go to for loop to get each text from hastags array

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"

check this [__NSCFArray objectForKey:]: unrecognized selector sent to instance

This might helps you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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