How to stop enumerateObjectsUsingBlock Swift How to stop enumerateObjectsUsingBlock Swift swift swift

How to stop enumerateObjectsUsingBlock Swift


This has unfortunately changed every major version of Swift. Here's a breakdown:

Swift 1

stop.withUnsafePointer { p in p.memory = true }

Swift 2

stop.memory = true

Swift 3

stop.pointee = true


In Swift 1:

stop.withUnsafePointer { p in p.memory = true }

In Swift 2:

stop.memory = true

In Swift 3 - 4:

stop.pointee = true


since XCode6 Beta4, the following way can be used instead:

let array: NSArray = // the array with some elements...array.enumerateObjectsUsingBlock( { (object: AnyObject!, idx: Int, stop: UnsafePointer<ObjCBool>) -> Void in        // do something with the current element...        var shouldStop: ObjCBool = // true or false ...        stop.initialize(shouldStop)        })