views:

123

answers:

2
counter = [myArray count];
for (i = 0 ; i < count ;  i++) {
     [[anotherArray objectAtIndex:i] setTitle:[myArray objectAtIndex:i]];
}

I would like to do it the objective C 2.0 way but can't seem to find how to access the index 'i' (if that's possible anyway).

for (NSString *myString in myArray) {
     [[anotherArray objectAtIndex:i] setTitle: myString];
}

(Please forgive any typos; I'm currently not behind my Mac so this is out of my head.)

+6  A: 

To do this you have to keep track of the index yourself:

NSUInteger index = 0;
for (NSString *myString in myArray) {
    [[anotherArray objectAtIndex: index] setTitle: myString];
    ++index;
}

Maybe in this case an old-fashioned for loop with an index is the better choice though. Unless you are trying to use fast enumeration for performance reasons here.

But it would be probably even better to restructure the code so that you don’t have to copy the strings from myArray to the title property of the objects in anotherArray manually.

Sven
I agree but I don't see how. Let me explain: the strings are used for localization. One array is read from a bundle, and those strings are assigned to the title properties of tabBarItems in a tabBar. (I prefer not to make localized xib files as this would create a lot of extra work each time a control is added > connect IBOutlets + IBActions, so I took the path of using localized plists...)
Glenn
I see. You might want to look at `NSLocalizedStringFromTable` and strings files instead of building your own localization functions on top of property lists. You could also try this: http://wilshipley.com/blog/2009/10/pimp-my-code-part-17-lost-in.html
Sven
+5  A: 

Use a block.

[myArray enumerateObjectsUsingBlock ^(id obj, NSUInteger idx, BOOL *stop) {
        [[anotherArray objectAtIndex: idx] setTitle: obj];
}];
bbum