Search code examples
objective-ccocoaobjective-c++

in statement in for loop


i have a for loop state ment as under:

    for(NSString* name in nameArray)

nameArray is NSArray.

In the above statement, what does it mean for: NSString* name in nameArray


Solution

  • Iterate through all NSString* in nameArray. Can be written less cleanly:

    for (int i=0;i<[nameArray count];++i) {
        NSString *name = [nameArray objectAtIndex:i];
        // Do stuff
    }
    

    Keep in mind: Don't iterate a mutable array and mutate it (and make sure no other thread does). In such a case you need to call count every iteration like displayed above.