How to set CMutablePointer to false in Swift?

后端 未结 3 771
醉酒成梦
醉酒成梦 2021-02-14 02:35

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

相关标签:
3条回答
  • 2021-02-14 02:43

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

    0 讨论(0)
  • 2021-02-14 02:58

    in Swift 5:

    stop.pointee = true
    
    0 讨论(0)
  • 2021-02-14 03:03

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

    stop.memory = true
    

    Or, as holex noted, you can:

    stop.initialize(true)
    
    0 讨论(0)
提交回复
热议问题