问题
This is how I initialize my NSMutablearray. I copy the content of cityList into cityArray. Both are NSMutableArray. FYI, I declared the'cityArray' using extern.
cityArray = [[NSMutableArray alloc] init];
[cityArray addObjectsFromArray:cityList];
Then, in other method in another class, I tried to to add one more object into 'cityArray' This is how I do it. But it keep crashing when it run this line of code.
NSString *allCities = @"All Cities";
[[cityArray valueForKey:@"cityName"] addObject:allCities];
This is from the console '[__NSArrayI addObject:]: unrecognized selector sent to instance 0x6292de0''.
But if I test it with setvalue:forkey:, it works but that is not what I want. I want the new data to be added into the bottom of the list.
回答1:
An NSArray
doesn't implement valueForKey:
. Your want either an NSDictionary
or use objectAtIndex:
.
Your second section of code should read:
NSString *allCities = @"All Cities";
[cityArray addObject:allCities];
来源:https://stackoverflow.com/questions/6519492/nsmutablearray-addobjects-unrecognized-selector