NSMutableArray addObjects - 无法识别的选择器 [英] NSMutableArray addObjects - unrecognized-selector
问题描述
这是我初始化NSMutablearray的方法。我将cityList的内容复制到cityArray中。两者都是NSMutableArray。仅供参考,我使用extern声明了'cityArray'。
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];
然后,在另一个类的其他方法中,我试图在'cityArray'中再添加一个对象我就是这样做的。但是当它运行这行代码时它会继续崩溃。
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];
这是来自控制台'[__ NSArrayI addObject:]:无法识别的选择器发送到实例0x6292de0''。
This is from the console '[__NSArrayI addObject:]: unrecognized selector sent to instance 0x6292de0''.
但如果我用setvalue:forkey:测试它,它可以工作,但这不是我想要的。我希望将新数据添加到列表的底部。
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.
推荐答案
NSArray
未实现 valueForKey:
。您需要 NSDictionary
或使用 objectAtIndex:
。
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];
这篇关于NSMutableArray addObjects - 无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!