UnsafePointer <CGAffineTransform> of CGAffineTransform

I am trying to create CGPath in Swift. I am using CGPathCreateWithRect(rect, transformPointer) .

How can I get UnsafePointer<CGAffineTransform> from CGAffineTransform ? I tried this:

  let transform : CGAffineTransform = CGAffineTransformIdentity let transformPointer : UnsafePointer<CGAffineTransform> = UnsafePointer(transform) 

I also tried this:

  let transform : CGAffineTransform = CGAffineTransformIdentity let transformPointer : UnsafePointer<CGAffineTransform> = &transform 

but Swift complains about '&' with non-inout argument of type... I also tried passing &transform directly to CGPathCreateWithRect , but this stops with the same error.

When I pass transform directly, Swift "Cannot convert a value of type" CGAffineTransform "to the expected argument type" UnsafePointer ".

What happens and how to do it with Swift 2.1?

+6
source share
2 answers

@Martin R provides a better answer, but as an alternative, I like to use my unsafe mutable pointers this way if you need to change the actual pointer in the future

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

I also tried passing & converting directly to CGPathCreateWithRect ...

You were almost there. transform must be a variable to pass it as an inout argument with & :
 var transform = CGAffineTransformIdentity let path = CGPathCreateWithRect(CGRect(...), &transform) 

For more information, see “Interacting with the API API” in the “Using Swift with Cocoa and Objective-C” section.


In Swift 3, it will be

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

or, to transform identity, simply

 let path = CGPath(rect: rect, transform: nil) 
+13
source

All Articles