I'm trying to create a CGPath in Swift. I'm using CGPathCreateWithRect(rect, transformPointer)
.
How can I get an UnsafePointer<CGAffineTransform>
from a CGAffineTransform
? I've tried this:
let transform : CGAffineTransform = CGAffineTransformIdentity
let transformPointer : UnsafePointer<CGAffineTransform> = UnsafePointer(transform)
I've also tried this:
let transform : CGAffineTransform = CGAffineTransformIdentity
let transformPointer : UnsafePointer<CGAffineTransform> = &transform
but Swift complains about a '&' with non-inout argument of type...
. I've also tried passing &transform
directly into CGPathCreateWithRect
but that stops with same error.
When I pass in transform
directly, Swift "Cannot convert value of type 'CGAffineTransform' to expected argument type 'UnsafePointer'".
What's going on, and how can make this work with Swift 2.1?
I've also tried passing &transform directly into CGPathCreateWithRect ...
You were almost there. transform
needs to be a variable
in order to pass it as an inout argument with &
:
var transform = CGAffineTransformIdentity
let 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.identity
let 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))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With