简体   繁体   中英

Objective-c “in” operator

I have noticed you can use "in" in objective c in xcode.

What does "in" mean? and how can it be used in code? Could not find anything in google since "in" can be used in normal sentences and the results are very vague.

“in”用于快速枚举 obj-c集合的语法

Here is the official Apple documentation on the "in" operator .

Basically, the construct

for(SomeClass* anObject in aCollection){
      ....
}

goes over all objects in a collection aCollection .

The in keyword is used for iterating over objects that conform to NSFastEnumeration . For example, to iterate over an array:

NSArray *a = [NSArray arrayWithObjects: @1, @2, @3, nil];

for (NSNumber *n in a) {
  // Do something with n
}

Are you talking about:

for (NSObject* object in myArray)
{

}

This is a fast enumeration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM