【问题标题】:NSInvalidArgumentException: "Unrecognized selector sent to instance"NSInvalidArgumentException:“无法识别的选择器发送到实例”
【发布时间】:2013-12-26 09:07:37
【问题描述】:

我遇到了一个我并不真正理解原因的问题。该异常没有给我提供理解问题的线索。

我想根据myArray中给出的数据在我的界面修改UILabel的内容。但是,正如我在函数“cellForRowAtIndexPath”中指定的行,程序会触发异常。

这个问题的原因是什么?

@property (strong, nonatomic) NSMutableArray *myArray; //

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [myArray addObject:@{@"field1": @"myfield1"}]
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return self.myArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    UILabel *myLabel = (UILabel *)[cell viewWithTag:100]; // myLabel is successfully created with the given viewWithTag 
    NSLog(@"Object at indexpath.row: %ld", (long)indexPath.row); // Object at indexpath.row: 0
    NSLog(@"The obj of the array = %@",[self.myArray objectAtIndex:indexPath.row] ); // The obj of the array = {field1: @"myfield1"}

    myLabel.text = [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"field1"]; // this part fires the exception given below.

    return cell;
}

//getter for myArray
-(NSMutableArray *)myArray{
    if(! _myArray){
        _myArray = [[NSMutableArray alloc] init];
    }
    return _myArray;
}

错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1459d1f0'

【问题讨论】:

  • 放断点和异常断点..
  • self.myArray 包含 NSArray 的实例,而不是 NSDictionary。您不能将objectForKey: 发送到数组。
  • 你不能在数组中使用objectforkey

标签: ios objective-c uitableview


【解决方案1】:

我不会告诉您要更改代码的内容,而是给您一些建议,以便您将来能够更轻松地解决问题。

首先,你得到的错误信息是这样的

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1459d1f0'

异常信息是这里的重点

unrecognized selector sent to instance

如果您使用您选择的搜索引擎搜索此消息,您会发现这意味着您正在调用一个不响应该方法的对象的方法。错误消息还会告诉您您尝试调用的方法,以及您正在调用的对象类型。

[__NSCFArray objectForKey:]

如果您查看 NSArray 对象的文档,您会发现没有可用的 objectForKey 方法。

你现在应该做的是在你的代码中设置一个断点(如果你不知道断点,那就去阅读它们 - 它们对于调试很重要)并逐步执行,直到你遇到抛出例外。此时,您可以检查您拥有的对象并查看其类型。然后,您应该能够弄清楚应该如何处理编码。

【讨论】:

  • 当我在函数“cellForRowAtIndexPath”中破坏我的代码并编写“po indexPath.row”时,lldb 会触发错误:property 'row' not found on object of type 'NSIndexPath *' error: 1 errors parsing expression 当我不破坏代码并通过 NSLog 对其进行评论时,它工作正常。但是为什么我破解密码时打印不出来呢?
【解决方案2】:

[__NSCFArray objectForKey:]: 无法识别的选择器发送到实例 0x1459d1f0'

你不能为 NSMutableArray 调用 objectForKey 如果你需要使用 objectForKey,也许你应该使用 NSDictionary: 或者您可以使用数组数组 例如:

NSArray *array = [[NSArray alloc] initWithObjects:@"field1Value",@"field2Value",@"field3Value",nil];

[self.myArray addObject:array];

现在,当您需要检索某些字段值时,只需调用数组中的索引

NSArray *array = [self.myArray objectAtIndex:[indexPath row]];
NSString* valueField1 =[array objectAtIndex:0];

希望这会有所帮助:)

编辑: 如果你需要使用 NSDictionary

    self.myArray=[[NSMutableArray alloc]init];
    dict=[[NSDictionary alloc] initWithObjectsAndKeys:@"value",@"KeyName",nil];
    [self.myArray addObject:dict]; 
    myLabel.text = [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"KeyName"];

【讨论】:

    【解决方案3】:

    这个异常并没有给我一个理解问题的线索。

    我不同意,异常告诉你你做错了什么。它告诉您您将objectForKey: 发送到数组而不是字典。我看到你使用objectForKey: 的唯一一行就是这一行。

    myLabel.text = [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"field1"]; 
    

    这意味着[self.myArray objectAtIndex:indexPath.row] 是一个数组,而不是字典。我不知道这是怎么发生的,因为在您向我们展示的代码中没有任何地方可以将对象添加到self.myArray。特别是,这不会:

    [myArray addObject:@{@"field1": @"myfield1"}]
    

    应该说

    [self.myArray addObject:@{@"field1": @"myfield1"}];
    

    我怀疑这只是一个复制错误,因为您也忘记了分号。请注意,仅在 myArray 前面加上下划线没有任何好处,因为您依赖访问器来初始化数组。

    【讨论】:

      【解决方案4】:

      如果您将NSDictionaryNSMutableDictionary 存储到数组中,那么代码将为您工作。如果你想将 NSDictionary 添加到数组中,你应该使用:[[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]

      【讨论】:

        【解决方案5】:

        对于遇到类似异常的任何人,请检查您的所有 IBActions 和 IBOutlets! 我删除了一个功能,但忘记右键单击 Main.story 板上的按钮以删除那里的出口/动作。

        下面的例子: Here we have 2 outlets, called "test" and "test2" respectfully, for the button in the upper left corner

        Here I deleted the code for "test2" outlet, but when I rightclick in the button the reference is still there.

        【讨论】:

        • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
        猜你喜欢
        • 2012-07-16
        • 2018-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 2012-02-21
        • 2015-08-02
        相关资源
        最近更新 更多