Search code examples
swift3cgpath

CGPath not working in Swift 3


I am updating code for swift 3. I have created a custom path for my spritekit object that works in swift 2. However, now I get a compiler error:

Nil is not compatible with expected argument type 'Unsafe Pointer CGAffineTransform'

let offsetX = player.size.width * player.anchorPoint.x
let offsetY = player.size.height * player.anchorPoint.y

let path = CGMutablePath()

CGPathMoveToPoint(path, nil, 10 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 10 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 0 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 24 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 28 - offsetX, 40 - offsetY)
CGPathAddLineToPoint(path, nil, 18 - offsetX, 46 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 36 - offsetY)
CGPathAddLineToPoint(path, nil, 6 - offsetX, 18 - offsetY)



path.closeSubpath()

The error is for passing nil in the second argument when adding to the path. I attempted to pass in an unsafe pointer as so:

var tr = CGAffineTransform.identity
CGPathMoveToPoint(path, &tr, 10 - offsetX, 16 - offsetY)
....

but then got another strange error.

CGPathMoveToPoint is unavailable. Use move(to: transform:)

However, there was no move function with argument name to. There was a move(toParent: ) however.


Solution

  • Most of the syntaxes are changed in Swift 3, But they didn't removed any API methods(like CGPathMoveToPoint) and It's just renamed like below.

    let path = CGMutablePath()
    path.move(to: CGPoint(x: 10.0, y: 10.0))
    path.addLine(to: CGPoint(x: 10.0, y: 10.0))