NSInvalidArgumentException',原因:' - [__ NSArrayI长度:无法识别的选择器发送到实例

时间:2014-04-26 23:26:09

标签: ios objective-c cocoa-touch uitableview unrecognized-selector

当我从谷歌寻找答案时,大多数答案都表示你试图在不受支持的NSArray上使用长度。

这里的问题是我甚至不在我的代码中使用任何NSArray或长度。

我有一个

NSMutableArray *filteredContent;   

其中filteredContent将包含来自plist的字典。

cell.textLabel.text的单元格上写tableView之前,一切都很顺利 选中NSLog,内容确实是一个数组。

这是我尝试编写单元格文本的方式:

cell.textLabel.text=[[self.filteredContent objectAtIndex:indexPath.row] valueForKey:@"recipeName"];

但它给了我错误,所以我改成了:

NSString *myValue = [self.filteredContent valueForKey:@"recipeName"];
cell.textLabel.text=myValue;

然而结果是一样的。我不知道我得到了什么错误。

进一步详情:

[results addObject:@[recipe]];

这是我创建主数组的地方,而不是通过segue PageView将其传递给filteredContent = results


编辑:

recipe = arrayOfPlist[i];其中arrayOfPlist

NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path];
//Output to NSLog(@"FIltered Content :  %@", self.filteredContent);
FIltered Content :  (
        (
            {
                category = main;
                difficultyLevel = "3/5";
                numberOfPerson = 5;
                recipeDetail = "Bulguru koy su koy beklet pisir ye ";
                recipeImage = "nohutlu-pilav.jpg";
                recipeIngredients = "pirinc,tereyag tuz,bulgur";
                recipeName = "Bulgurlu Pilav";
                time = "25 dk";
            }
        ),
        (
            {
                category = main;
                difficultyLevel = "3/5";
                numberOfPerson = 5;
                recipeDetail = "Bulguru koy su koy beklet pisir ye ";
                recipeImage = "nohutlu-pilav.jpg";
                recipeIngredients = "pirinc,tereyag tuz,bulgur";
                recipeName = "Bulgurlu Pilav";
                time = "25 dk";
            }
        )
    )

    2014-04-27 02:47:41.704 deneme2[19820:60b] VALUE IN FILTERED TABLE  is (
        "Bulgurlu Pilav" // this is what i want to write to the cell label and i get it with myValue-look a bit above
    )

2 个答案:

答案 0 :(得分:5)

根据您的数据输出,您有一个额外的数组。所以你想要这个:

cell.textLabel.text = self.filteredContent[indexPath.row][0][@"recipeName"];

filteredContentArray的每个元素都是另一个数组。每个内部数组都有一个包含所需数据的字典。

答案 1 :(得分:1)

做@rmaddy的建议或:

变化:

[results addObject:@[recipe]];

为:

[results addObject:recipe]

说明:

  

NSInvalidArgumentException',原因:' - [__ NSArrayI长度:无法识别的选择器发送到实例

此错误告诉我们:

  1. 对象是NSArray某处,某种程度上
  2. NSArray对象调用的方法是length
    • NSArray 拥有length方法
    • 似乎某处发送了对length方法的调用,很可能是在NSString对象实际上持有NSArray对象而不是NSString对象。 / LI>
  3. 首先请注意,当您指定:

    1. @[]
    2. 所以当你这样做时:[results addObject:@[recipe]];

      • @[recipe]
        相当于
      • [NSArray alloc] initWithObjects:recipe, nil]

      所以...你基本上把recipe,把它放在一个数组中,然后在results中添加这个数组对象(是另一个数组

      稍后来PageView

      [[self.filteredContent objectAtIndex:indexPath.row] valueForKey:@"recipeName"];
      

      将返回键recipeName的值数组。

      基本上,你的字符串是一个数组,而不是字符串对象 现在......无论您是否在其上调用了length,在生命周期的某个地方,length都会调用cell.textLabel.text并引发此错误。

相关问题