LoginSignup
3
3

More than 5 years have passed since last update.

ObjC to Swift : [NSArray enumerateObjectsUsingBlock:]

Last updated at Posted at 2015-05-04

Objective-C

NSArray * array = @[ obj0, obj1, ... ];

[array enumerateObjectsUsingBlock: ^( id object,
                                      NSUInteger index,
                                      BOOL * stop )
{
    // do some stuff for each "object" at "index"
}


Swift 2.0 (using "object" and "index" inside the loop)

let swiftArray: Array = [ obj0, obj1, ... ]

for ( index, object ) in swiftArray.enumerate()
{
    // do some stuff for each "object" at "index"
    // no explicit "stop" parameter ...
}


Swift 1.2 (using "object" and "index" inside the loop)

let swiftArray: Array = [ obj0, obj1, ... ]

for ( index, object ) in enumerate( swiftArray )
{
    // do some stuff for each "object" at "index"
    // no explicit "stop" parameter ...
}

Swift (using only "object" inside the loop)

for object in swiftArray
{
    // do some stuff for each "object"
}

Swift (using no object information inside the loop)

for _ in swiftArray
{
    // do some stuff swiftArray.count times
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3