OGeek|极客世界-中国程序员成长平台

标题: ios - NSInvalidArgumentException 有一个字典 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:13
标题: ios - NSInvalidArgumentException 有一个字典

我正在尝试将 NSDictionary 数据放入 tableView。但是当只有一个值时我会遇到问题。

这里,有一个“上传”计数返回 8(但它应该返回 1),如果有多个“上传”计数返回“上传”的数量,因为它应该在第一种情况下。

NSDictionary *jsonDictionary = aDicLog;
NSMutableArray *tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];

int count = [tableData count];
NSLog(@"COUNT: %i", count);

表格

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return [tableData count];
}

单元格

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{      
   //App Crashes in this line 
   NSDictionary *ldUpload = [tableData objectAtIndex:indexPath.row];
   ...
   return cell;
}

异常(exception),有一个“上传”:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance

字典如下所示:

这是应用崩溃的时间只需一次“上传”)

Printing description of tableData:
{
    FileName =     {
        text = "IMG_0510.MOV";
    };
    status =     {
        text = 2;
    };
    Event =     {
        text = Q;
    };
    Date =     {
        text = "9/9/2016 11:01:58 AM";
    };
    Publicacion =     {
        text = "Example";
    };
    Seccion =     {
        text = Sistemas;
    };
    idMultimedia =     {
        text = 1136;
    };
    text = "";
}

这是应用运行良好的情况有多个“上传”)

Printing description of tableData:
<__NSArrayM 0x7a49dc90>(
{
    FileName =     {
        text = "1-FB5A9792.MOV";
    };
    status =     {
        text = 4;
    };
    Event =     {
        text = Test;
    };
    Date =     {
        text = "9/7/2016 9:06:21 AM";
    };
    Publicacion =     {
        text = "Example";
    };
    Seccion =     {
        text = Sistemas;
    };
    idMultimedia =     {
        text = 993;
    };
    text = "";
},
{
    FileName =     {
        text = "2-FB5A9793.MOV";
    };
    status =     {
        text = 4;
    };
    Event =     {
        text = Test;
    };
    Date =     {
        text = "9/7/2016 9:06:21 AM";
    };
    Publicacion =     {
        text = "Example";
    };
    Seccion =     {
        text = Sistemas;
    };
    idMultimedia =     {
        text = 994;
    };
    text = "";
},...

我正在使用 XMLReader将 XML 转换为 Dictionary,然后将上述数据传递给 tableData

XML

<multimedia>
  <uploads>
    <upload>
      <id>1136</id>
      <Date>9/9/2016 11:01:58 AM</Date>
      <FileName>IMG_0510.MOV</FileName>
      <status>2</status>
      <ublicacion>Example</Publicacion>
      <Seccion>Sistemas</Seccion>
      <Event>Q</Event>
    </upload>
  </uploads>
</multimedia>

当只有 1 个“上传”和 n 个“上传”的情况下,我如何返回 1 而不是 8?



Best Answer-推荐答案


您可以在生成tableData数组的基础上检查对象类型是Dictionary还是NSArray

id uploadsObj = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
if ([uploadsObj isKindOfClass:[NSDictionary class]]) {
    NSMutableArray *tableData = [NSMutableArray arrayWithObjectNSDictionary*)uploadsObj];
}
else if ([uploadsObj isKindOfClass:[NSArray class]]) {
    NSMutableArray *tableData = [NSMutableArray arrayWithArrayNSArray*)uploadsObj];
}

关于ios - NSInvalidArgumentException 有一个字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728785/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4