How do I stop a block enumeration?
myArray.enumerateObjectsUsingBlock( { object, index, stop in
//how do I stop the enumeration in here??
})
I know in obj-c you do this:
[myArray enumerateObjectsUsingBlock:^(id *myObject, NSUInteger idx, BOOL *stop) {
*stop = YES;
}];
In Swift 1:
In Swift 2:
In Swift 3 - 4:
Just
stop = true
Since stop is declared as inout, swift will take care of mapping the indirection for you.
since XCode6 Beta4, the following way can be used instead:
This has unfortunately changed every major version of Swift. Here's a breakdown:
Swift 1
Swift 2
Swift 3
The accepted answer is correct but will work for NSArrays only. Not for the Swift datatype
Array
. If you like you can recreate it with an extension.call it like
or for function with a block as the last parameter you can do