【问题标题】:'-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0''-[__NSCFString objectForKey:]: 无法识别的选择器发送到实例 0x9c515c0'
【发布时间】:2014-05-23 13:12:04
【问题描述】:

先生的问题是-----我正在从 url 和 arrayobj 中获取数据。当我运行这个项目时,我得到了这个错误。我做错了什么,项目中的语句或代码请检查connectiondidfinishloadingcellforrowanindexpath

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];

newsobj=[dictobj objectForKey:@"news"];

//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];

 for (NSDictionary *dict in  newsobj)
 {
    NSDictionary *title=[dict objectForKey:@"title"];
    [arrayobj addObject:title];
 }
 [[self tableobj]reloadData];
}

//这是索引路径代码处的行单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
    [[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text=[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"];
return newcustomobj;
}

错误是:

2014-05-23 18:34:04.913 newsdata[6518:90b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0
2014-05-23 18:34:04.915 newsdata[6518:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0'
*** First throw call stack:
(
0   CoreFoundation                      0x01b851e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x015cb8e5 objc_exception_throw + 44
2   CoreFoundation                      0x01c22243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x01b7550b ___forwarding___ + 1019
4   CoreFoundation                      0x01b750ee _CF_forwarding_prep_0 + 14
5   newsdata                            0x00008a13 -[ViewController tableView:cellForRowAtIndexPath:] + 435
6   UIKit                               0x0038411f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7   UIKit                               0x003841f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
8   UIKit                               0x00365ece -[UITableView _updateVisibleCellsNow:] + 2428
9   UIKit                               0x0037a6a5 -[UITableView layoutSubviews] + 213
10  UIKit                               0x002fa964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11  libobjc.A.dylib                     0x015dd82b -[NSObject performSelector:withObject:] + 70
12  QuartzCore                          0x03dcd45a -[CALayer layoutSublayers] + 148
13  QuartzCore                          0x03dc1244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14  QuartzCore                          0x03dc10b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
15  QuartzCore                          0x03d277fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
16  QuartzCore                          0x03d28b85 _ZN2CA11Transaction6commitEv + 393
17  QuartzCore                          0x03d29258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
18  CoreFoundation                      0x01b4d36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
19  CoreFoundation                      0x01b4d2bf __CFRunLoopDoObservers + 399
20  CoreFoundation                      0x01b2b254 __CFRunLoopRun + 1076
21  CoreFoundation                      0x01b2a9d3 CFRunLoopRunSpecific + 467
22  CoreFoundation                      0x01b2a7eb CFRunLoopRunInMode + 123
23  GraphicsServices                    0x03c8f5ee GSEventRunModal + 192
24  GraphicsServices                    0x03c8f42b GSEventRun + 104
25  UIKit                               0x0028bf9b UIApplicationMain + 1225
26  newsdata                            0x000119cd main + 141
27  libdyld.dylib                       0x02330725 start + 0
28  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

  • 我的猜测是在不知道 JSON 的情况下(您在问题中出现):[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"]; 何时调用 objectForKey(第一个或第二个,因为我们不知道 JSON) ,实际上您的对象是 NSString 而不是 NSDictionary
  • 我认为您必须在此处粘贴您的 JSON 格式,以便其他人可以帮助您纠正错误。
  • 首先感谢您的回复,我在 json 格式中做错了。但是我学习并阅读了一些 json 主题,一位朋友建议我这样做可以轻松解决您的问题...

标签: ios objective-c nsarray


【解决方案1】:

你假设的变量dictobj是一个NSDictionary,实际上它是一个NSString。由于字符串不响应 objectForKey: 方法,您的应用程序崩溃了。这就是错误消息告诉您的内容。检查此link。了解更多详情。

【讨论】:

  • 当我使用此代码时,我可以解决这些错误 [__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0 2014-05-23 18:34:04.915 newsdata[6518:90b] *** Terminating应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“-[__NSCFString objectForKey:]:无法识别的选择器发送到实例 0x9c515c0”***第一次抛出调用堆栈:
【解决方案2】:

您认为它是字典的对象之一实际上是一个字符串。

尤其是在解析 JSON 或 XML 时:始终仔细检查您获取并继续使用的对象的类。使用[object isKindOfClass:[ClassIThinkItIs class]]

【讨论】:

    【解决方案3】:
    cell.lblTitle.text = [[dataObjectArray objectAtIndex:indexPath.row]title];
    
    cell.datobj.text = [[dataObjectArray objectAtIndex:indexPath.row]date];
    
    cell.longdicobj.text = [[dataObjectArray objectAtIndex:indexPath.row]long_desc];
    
    [cell.imageobj setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dataObjectArray objectAtIndex:indexPath.row]image_file]]]
          placeholderImage:[UIImage imageNamed:@"songs85-56.png"]];
    

    【讨论】:

      【解决方案4】:

      我认为json数据返回是news键字典的新闻字典数组,每个news字典都有一个title键。所以你可以尝试下面的代码在cellForRow中显示title

      - (void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
      NSError *error ;
      NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];
      
      newsobj=[dictobj objectForKey:@"news"];
      
      //NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];
      
       for (NSDictionary *dict in  newsobj)
       {
          NSString *title=[dict objectForKey:@"title"]; //title may be NSString type
          [arrayobj addObject:title];
       }
       [[self tableobj] reloadData];
      }
      

      在 CellForRow 中

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      static NSString*cellide=@"identifier";
      newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
      if (newcustomobj==nil)
      {
          [[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
      }
      newcustomobj.newstitle.text= [arrayobj objectAtIndex:0]; //just get title of first news, you can replace 0 with indexpath.row to show news for row.
      return newcustomobj;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多