【问题标题】:Reading Element in NSMutableArray读取 NSMutableArray 中的元素
【发布时间】:2015-03-09 04:35:14
【问题描述】:

我基于 Twitter 推文中的 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);
}

【问题讨论】:

  • 您的错误只是说您正在向 nsarray 发送消息 objectforkey 实际上您只能将此消息 objectforkey 传递给 nsDictionary 。

标签: ios objective-c iphone json nsmutablearray


【解决方案1】:

正如错误所说。

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

【讨论】:

  • 既然它返回了一个完整的对象,我将如何访问返回的所有主题标签中的各个元素
  • 我让你有 for 循环来访问所有的 hasthags。代码已经存在 for 循环。
【解决方案2】:

数组并包含字典数组,因此如果您对NSArray 执行操作objectForKey 将崩溃并显示错误消息“无法识别的选择器”

检查这个[__NSCFArray objectForKey:]: unrecognized selector sent to instance

这可能对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多