Reputation: 1828
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'
Upvotes: 30
Views: 81828
Reputation: 193
To anyone facing a similar exception, check all your IBActions and IBOutlets! I deleted a function but forgot to right click on the button on the Main.story board to delete the outlet/action there.
example below: Here we have 2 outlets, called "test" and "test2" respectfully, for the button in the upper left corner
Upvotes: 2
Reputation: 3324
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]
Upvotes: 1
Reputation: 86651
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.
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:
[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.
Upvotes: 1
Reputation: 4276
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.
Upvotes: 43
Reputation: 1950
[__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"];
Upvotes: 2