Last active
August 29, 2015 14:02
-
-
Save bentrengrove/f930ba3233130aad1ace to your computer and use it in GitHub Desktop.
How to use the enumerateObjectsWithBlock method on NSArray with Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
} |
I don't think need to explicitly declare a return type of void: '-> void'.
Otherwise, this is cool!
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
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;
}
}