Swift 4 UnsafeMutablePointer ObjCBool parameter, how do you use it in a closure?

When enumerating the attributed string, there is a UnsafeMutablePointer<ObjCBool> parameter, and maybe you like me want to change it from false to true to stop something enumerating.

The syntax appears as follows;
{ (attributeDict:  [NSAttributedStringKey: Any], theRange: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in

enumerateAttributes(in:……)… has a closure that has a ‘stop’ element. You might first think “I need to use the inout” but soon Xcode will flag this RED.

{ (attributeDict, theRange, stop) in
{ (attributeDict, theRange, _) in. // << many people skipped the last parameter by ignoring it

 

 

The quick info you need;

You simple access the ‘stop’ elements first element [0] and set it to ‘true’ to exit the closure/block of code.

‘stop[0]=true’

Here is the whole gist of the situation.

eSpecialized's Blog

Bill Thompson

I am a mobile smartphone and embedded Linux engineer. I love to tinker with electronics and fly drones in spare time when not geeking out in a developers world!