Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInvalidArgumentException: "Unrecognized selector sent to instance"

Tags:

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.

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' 
like image 566
ankakusu Avatar asked Dec 09 '13 09:12

ankakusu


2 Answers

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.

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.

like image 131
Nick Bull Avatar answered Oct 07 '22 20:10

Nick Bull


[__NSCFArray objectForKey:]: unrecognized selector sent to instance 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:

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

    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"]; 
like image 30
Iphone User Avatar answered Oct 07 '22 21:10

Iphone User