UnsafePointer<CGAffineTransform> from CGAffineTransform UnsafePointer<CGAffineTransform> from CGAffineTransform swift swift

UnsafePointer<CGAffineTransform> from CGAffineTransform


I've also tried passing &transform directly into CGPathCreateWithRect ...

You were almost there. transform needs to be a variablein order to pass it as an inout argument with &:

var transform = CGAffineTransformIdentitylet path = CGPathCreateWithRect(CGRect(...), &transform)

For more information, see "Interacting with C APIs"in the "Using Swift with Cocoa and Objective-C" documentation.


In Swift 3 this would be

var transform = CGAffineTransform.identitylet path = CGPath(rect: rect, transform: &transform)

or, for the identity transform, just

let path = CGPath(rect: rect, transform: nil)


@Martin R provides the best answer, but as an alternative, I like to use my unsafe mutable pointers this way, in case you need to alter the actual pointer in the future

let path = withUnsafeMutablePointer(&transform){  CGPathCreateWithRect(CGRect(...), UnsafeMutablePointer($0))}