12

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.

Binarian
  • 12,296
  • 8
  • 53
  • 84
Dimillian
  • 3,616
  • 4
  • 33
  • 53
  • BTW, you set this to `true` when you want to stop, not `false`. If you want it to continue, you simply leave it alone. – Rob Aug 01 '14 at 22:37

3 Answers3

17

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.)

Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
  • 2
    Haha, I love seeing what people come up with operator overloading...this is a great one! – Jack Jun 10 '14 at 14:16
  • Don't worry about the downvoter, he must be an ass. Yours is the most enlightening answer I've read about Swift since it was announced. +1 from me, anyways. – original_username Jul 10 '14 at 11:35
9

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

stop.memory = true

Or, as holex noted, you can:

stop.initialize(true)
Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • If you want to post your own answer here, I'm happy to remove mine. I'd rather see you get the reputation points! – Rob Aug 01 '14 at 16:09
  • 1
    thank you, that is very generous from you! I'm not chasing credits, I'm glad to help only. :) just for fun, you know... :) – holex Aug 01 '14 at 16:12
1

in Swift 5:

stop.pointee = true
nathanwhy
  • 5,884
  • 1
  • 12
  • 13