将NSArray中包含的字符串分配给包含在不同NSArray中的对象

时间:2014-10-22 21:58:22

标签: objective-c cocoa loops nsarray

我有这两个数组:

NSArray *nameArray = [NSArray arrayWithObjects:@"Cow", @"Haystack", @"Cow Bell", @"Branding Iron", @"Herding Dog",
                              @"Camel", @"Tractor", @"Warehouse", @"Milking Pipeline", @"Robotic Milker", @"Amusement Park",
                              @"Nitrous Kit", @"Mooship", nil];
NSArray *itemArray = [NSArray arrayWithObjects:cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10, cell11, cell12, cell13, nil];

单元格都是CCLabelTTF个对象,因此它们都有一个字符串属性。我想在itemArray中为nameArray中的相应字符串分配标签。我查了documentation,我认为我需要使用的是- (void)makeObjectsPerformSelector:(SEL)aSelector,但我并不完全确定我需要用作选择器。

我正在使用for循环来尝试这样做:

for (int i = 0; i <= itemArray.count; i++) {
    [itemArray makeObjectsPerformSelector:@selector()];
    i++;
}

我是否正确使用makeObjectsPerformSelector:?如果没有,那么我需要使用什么?

1 个答案:

答案 0 :(得分:2)

你不会在循环中使用makeObjectsPerformSelect:。它为你做了循环。

但是,由于您需要从名称数组中提取正确的标签,因此使用makeObjectsPerformSelector:并不是一个真正的选项。

你想:

for (NSUInteger i = 0; i < itemArray.count; i++) {
    CCLabelTTF *cell = itemArray[i];
    NSString *label = nameArray[i];
    cell.someProperty = label;
}

另请注意在<声明中使用<=代替for