ios NSInvalidArgumentException:“无法识别的选择器发送到实例”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20467539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSInvalidArgumentException: "Unrecognized selector sent to instance"
提问by ankakusu
I'm facing a problem that I not truly understand the reason. The exception does not give me a clue to understand the problem.
我面临一个我不真正理解原因的问题。异常并没有给我提供理解问题的线索。
I want to modify content of UILabel at my interface according to the data given in myArray. However, as the line I specified at function "cellForRowAtIndexPath" the program fires an exception.
我想根据myArray中给定的数据在我的界面修改UILabel的内容。但是,正如我在函数“cellForRowAtIndexPath”中指定的那一行,程序会触发一个异常。
What is the reason for this problem?
这个问题的原因是什么?
@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;
}
The error:
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1459d1f0'
回答by Nick Bull
Rather than tell you what to change your code, I'll give you some pointers so you will hopefully be able to resolve your problems in the future a bit easier.
我不会告诉您更改代码的内容,而是给您一些提示,以便您将来能够更轻松地解决您的问题。
First, the error message you have is this
首先,您收到的错误消息是这样的
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1459d1f0'
The exception message is the key point here
异常消息是这里的关键点
unrecognized selector sent to instance
If you search for this message using the search engine of your choice, you'll see that this means you are calling a method on an object that doesn't respond to that method. The error message also tells you the method you are trying to call, and on which type of object you are calling it.
如果您使用您选择的搜索引擎搜索此消息,您将看到这意味着您正在对不响应该方法的对象调用方法。错误消息还会告诉您尝试调用的方法以及调用它的对象类型。
[__NSCFArray objectForKey:]
If you look at the documentation for the NSArray
object, you'll see that there is no objectForKey
method available for that.
如果您查看该NSArray
对象的文档,您会发现没有objectForKey
可用的方法。
What you should now do is set a breakpoint in your code (if you don't know about breakpoints, go off and read about them - they are IMPORTANT for debugging) and step through until you hit the line that is throwing the exception. At this point, you can inspect the objects you have and see what the types are. You should then be able to work out what you should do with the coding.
您现在应该做的是在您的代码中设置一个断点(如果您不知道断点,请继续阅读它们 - 它们对于调试很重要)并逐步执行,直到您遇到抛出异常的行。此时,您可以检查您拥有的对象并查看类型。然后你应该能够弄清楚你应该用编码做什么。
回答by Iphone User
[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1459d1f0'
[__NSCFArray objectForKey:]: 无法识别的选择器发送到实例 0x1459d1f0'
You can't call objectForKey for NSMutableArray Maybe you should use NSDictionary if you need to use objectForKey: or you can use array of arrays ex:
你不能为 NSMutableArray 调用 objectForKey 也许你应该使用 NSDictionary 如果你需要使用 objectForKey: 或者你可以使用数组,例如:
NSArray *array = [[NSArray alloc] initWithObjects:@"field1Value",@"field2Value",@"field3Value",nil];
[self.myArray addObject:array];
Now , when you need to retrieve some fields value, then just call the index in the array
现在,当您需要检索某些字段值时,只需调用数组中的索引
NSArray *array = [self.myArray objectAtIndex:[indexPath row]];
NSString* valueField1 =[array objectAtIndex:0];
Hope this will help:)
希望这会有所帮助:)
Edit: If you need to use NSDictionary
编辑:如果您需要使用 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"];
回答by JeremyP
The exception does not give me a clue to understand the problem.
异常并没有给我提供理解问题的线索。
I disagree, the exception tells you exactly what you did wrong. It tells you that you sent objectForKey:
to an array instead of to a dictionary. The only line where I see you using objectForKey:
is this one.
我不同意,异常告诉你你做错了什么。它告诉您发送objectForKey:
的是数组而不是字典。我看到你使用的唯一一行objectForKey:
是这个。
myLabel.text = [[self.myArray objectAtIndex:indexPath.row] objectForKey:@"field1"];
which means that [self.myArray objectAtIndex:indexPath.row]
is an array, not a dictionary. I don't know how that happened because nowhere in your code that you show us is there anything that adds an object to self.myArray
. In particular, this doesn't:
这意味着这[self.myArray objectAtIndex:indexPath.row]
是一个数组,而不是字典。我不知道这是怎么发生的,因为在您向我们展示的代码中没有任何地方可以将对象添加到self.myArray
. 特别是,这不会:
[myArray addObject:@{@"field1": @"myfield1"}]
It should say
应该说
[self.myArray addObject:@{@"field1": @"myfield1"}];
I suspect that was just a copy error though because you also forgot the semicolon. Note that just putting an underscore on the front of myArray
isn't any good because you rely on the accessor to initialise the array.
我怀疑这只是一个复制错误,因为你也忘记了分号。请注意,仅在前面加上下划线myArray
并没有任何好处,因为您依赖访问器来初始化数组。
回答by Abdullah Md. Zubair
If you store NSDictionary
or NSMutableDictionary
to array then then the code will work for you. If you want to add NSDictionary to array you should use: [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]
如果您存储NSDictionary
或NSMutableDictionary
数组,那么代码将为您工作。如果要将 NSDictionary 添加到数组,则应使用: [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil]