将NSArray的一部分添加到其他NSArray中

时间:2015-01-19 22:05:40

标签: ios objective-c nsmutablearray nsarray

我正在尝试将大NSArray的一部分添加到另一个NSArray。但是,我做错了,因为它一直在抛出错误。

这是具体的代码:

@property (nonatomic, strong) NSMutableArray *searches;
@property (nonatomic, strong) NSMutableArray *chosenResult;

...bit further down...

for (NSString *string in self.searches[indexPath.row]) {
    [self.chosenResult addObject:string];
}

这是错误:

'NSInvalidArgumentException', reason: '-[Search countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7a1710d0'

2 个答案:

答案 0 :(得分:2)

看起来你不需要快速枚举循环,因为你已经有了数组的索引:

[self.chosenResult addObject:self.searches[indexPath.row]];

您看到的错误是因为快速枚举循环期望某些集合,如数组,但您实际上正在为它提供一个Search对象,该对象不实现快速枚举方法。

答案 1 :(得分:2)

您认为数据结构中的内容与实际在数据结构中的内容之间存在脱节。该错误告诉您正在尝试将消息countByEnumeratingWithState:objects:count:(快速枚举方法)发送到Search对象。

这告诉我们你的self.searches数组包含Search个对象,而不是你所说的字典。 (或者至少目标索引处的对象是搜索对象。其他索引处的对象可能是其他类型...)我建议进行一些调试以找出数组为什么包含与您认为不同的东西。