开发者

in statement in for loop

开发者 https://www.devze.com 2023-02-20 18:35 出处:网络
i have a for lo开发者_开发问答op state ment as under: for(NSString* name in nameArray) nameArray is NSArray.

i have a for lo开发者_开发问答op 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


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.


This is fast enumeration syntax introduced in Objective-C 2.0. Check this tutorial for the details. Also you can Google "objective c fast enumeration" for many other resources available online.


It means that the code inside the parenthesis will be executed for every object in the nameArray, which you will access through the NSString *name variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消