I have some Objective-C code that does the job for me. But is it ugly (inefficient). Can it be performed better with for-each loops?
See this code please:
for (int i = 0; i < [careerIds count]; i++) {
NSString *titleString = [[titles objectAtIndex:i] stringValue];
if ([titleString isEqualToString:@""] || [titleString rangeOfString:@"Intresseanmälan"].location != NSNotFound) {
// Don't add the id
} else {
[ids addObject:[[careerIds objectAtIndex:i] stringValue]];
}
}
I don’t think the code that you have is particularly ugly — there’s nothing at all wrong with using an indexed for loop as you’ve done. The only thing I might change would be to invert the sense of the
ifstatement so that you avoid the empty// Don't add the idline. Here’s one way:To get to the heart of your question, no, I don’t believe it’s possible to use the fast enumeration version of the
forloop to iterate over the contents of two separate containers at the same time. You can use it with one, but as I pointed out in a comment, you then have to use-indexOfObject:to recover the index of the current object so that you can get the corresponding item from the other array using-objectAtIndex:.