NSMutableArray addObjects - unrecognized-selector

前提是你 提交于 2020-01-21 20:33:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!