1

I a using following code to create path from CGPoint array().

public func creatPath (for points:[CGPoint]) {
    let path = CGMutablePath()
    let startPoint = points.first

    CGPathMoveToPoint( path, CGAffineTransform.identity, startPoint.x, startPoint.y)

    var index = 0
    for point in points {
        if index == 0 { continue }
        CGPathAddLineToPoint(path, nil, points.x, points.y)
        index += 1
    }
    path.closeSubpath()
}

But it always end up showing me following error:

nil is not compatible with expected argument type UnsafePointer <CGAffineTransForm>

I have also tried using CGAffineTransform.identity Then it shows:

Cannot convert value of type 'CGAffineTransform' to expected argument type 'UnsafePointer<CGAffineTransform>'

I am not sure what else can we use here. I am using Xcode8 beta 6. Swift 3.0

Edit: When I try:-

var transform = CGAffineTransform.identity as CGAffineTransform;
CGPathMoveToPoint( path, &transform, startPoint.x, startPoint.y)

It shows:

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

And while trying:

CGPathMoveToPoint( path, NSNull(), startPoint.x, startPoint.y)

Error is: Cannot convert value of type 'NSNull' to expected argument type 'UnsafePointer'

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • 1
    It's because in Objective-C, we do: `CGAffineTransform transform = CGAffineTransformIdentity; CGPathAddLineToPoint(path, &transform, point.x, point.y);` with the "&var" stuff. If you want to pass `NULL`, apparently you can do like that: http://stackoverflow.com/a/27169902/1801544 (I don't speak Swift, I'm just pointing out possible solutions/hints). – Larme Sep 05 '16 at 09:13

1 Answers1

3

Swift 3 has a much improved, object-oriented interface for CGMutablePath. Best of all: the transform parameter has a default value and can be omitted.

public func creatPath (for points:[CGPoint]) {
    let path = CGMutablePath()
    let startPoint = points.first

    path.move(to: startPoint)

    var index = 0
    for point in points {
        if index == 0 { continue }
        path.addLine(to: point)
        index += 1
    }
    path.closeSubpath()
}

Note: I've changed the loop variable from points to point to avoid to collision with the parameter name.

Update

You can simplify your code:

public func creatPath (for points:[CGPoint]) {
    let path = CGMutablePath()
    path.addLines(between: points)
    path.closeSubpath()
}
Codo
  • 75,595
  • 17
  • 168
  • 206
  • This is great. Thank you very much for the help! Where can I get the documentation of Swift3? As https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGPath/#//apple_ref/c/func/CGPathCreateMutable still have it for Swift2.x, I guess. – rptwsthi Sep 05 '16 at 09:32
  • I'm also confused about the documentation as there is old and new one out there. Have a look at [CGMutablePath - Core Graphics](https://developer.apple.com/reference/coregraphics/cgmutablepath). It covers the Swift 3 syntax but has hardly any description. – Codo Sep 05 '16 at 09:41