How do you alloc/dealloc unsafe pointers in Swift?

后端 未结 4 1591
我寻月下人不归
我寻月下人不归 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 10:04

    As of Beta 5 you can just pass your variables prefixed with &. Just make sure to initialise them

    More info on https://developer.apple.com/swift/blog/?id=6

    To use single variables, you can just set them to a zero value

    var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
    color.getRed(&r, green: &g, blue: &b, alpha: &a)
    

    To use C arrays, you just create a normal array with the indices you expect

    var points:[NSPoint] = [NSPoint(), NSPoint(), NSPoint()] //notice how we set empty NSPoints
    self.elementAtIndex(index, associatedPoints: &points)
    CGPathAddCurveToPoint(path, nil, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y)
    

提交回复
热议问题