How do you alloc/dealloc unsafe pointers in Swift?

后端 未结 4 1659
我寻月下人不归
我寻月下人不归 2021-02-01 09:23

With Beta 4, I had this code that worked fine:

var red, green, blue, alpha: UnsafePointer
red = UnsafePointer.alloc(1)
green = Unsa         


        
4条回答
  •  天命终不由人
    2021-02-01 09:52

    This works, Swift is smart enough to know what to do with the & operator:

    let color = UIColor.purpleColor()
    var r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat = 0
    color.getRed(&r, green: &g, blue: &b, alpha: &a)
    CGContextSetRGBStrokeColor(c, r, g, b, a)
    

    If you really want to do the alloc yourself, use your favorite flavor and construct the pointer like this:

    let p = UnsafeMutablePointer(calloc(1, UInt(sizeof(CGFloat))))
    // later don't forget to free(p)
    

提交回复
热议问题