Skip to content

Instantly share code, notes, and snippets.

@bentrengrove
Last active August 29, 2015 14:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bentrengrove/f930ba3233130aad1ace to your computer and use it in GitHub Desktop.
Save bentrengrove/f930ba3233130aad1ace to your computer and use it in GitHub Desktop.
How to use the enumerateObjectsWithBlock method on NSArray with Swift
let nArray : NSArray = ["1", "2", "3"]
nArray.enumerateObjectsUsingBlock {(obj, index, stop) in
println("Object \(obj) Index \(index)")
if index == 1 {
stop.withUnsafePointer { $0.memory = true }
}
}
@aarondaub
Copy link

If you want the same behaviour in a more Swift-y way:

let array: NSArray = ["1", "2", "3"]

for (index, obj : AnyObject) in enumerate(array) {
println("Object (obj) Index (index)")
if(index == 1){
break;
}
}

@ArtSabintsev
Copy link

I don't think need to explicitly declare a return type of void: '-> void'.

Otherwise, this is cool!

@bentrengrove
Copy link
Author

Updated to remove unneeded syntax.

Just a note, this was never intended to be the way do enumerate over an array, just a academic exercise to try and use some C pointers in swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment