Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CMutablePointer<ObjCBool> to false in Swift?

Tags:

ios

swift

Basically I'm using the AssetsLibrary frameworks in Swift, how could I modify the value of the stop pointer to NO/False/0 (I don't even know what value it should except) ?

self.library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {(group: ALAssetsGroup!, stop: CMutablePointer<ObjCBool>) in

},
failureBlock: {(error: NSError!) in

})

I should be able to access the value and modify it with unsafePointer but I can't seems to be able to write the closure correctly.

like image 672
Dimillian Avatar asked Jun 10 '14 13:06

Dimillian


2 Answers

This is the equivalent of *stop = YES;:

stop.withUnsafePointer { $0.memory = true }

To make it more succinct, you could do things like:

operator infix <- {}

@infix func <- <T>(ptr: CMutablePointer<T>, value: T) {
    ptr.withUnsafePointer { $0.memory = value }
}

and then the line above becomes simply this:

stop <- true

Not sure if that's recommended style, though...

(You can choose characters from / = - + * % < > ! & | ^ . ~ to create custom operators.)

like image 85
Jean-Philippe Pellet Avatar answered Oct 07 '22 01:10

Jean-Philippe Pellet


As of Xcode 6 beta 4, you can now do:

stop.memory = true

Or, as holex noted, you can:

stop.initialize(true)
like image 27
Rob Avatar answered Oct 06 '22 23:10

Rob