unable to call CGPathMoveToPoint() with nil in Swift

3.4k views Asked by At

I have tried to call CGPathMoveToPoint() in Swift using the CGMutablePathRef and a CGPoint as input and NULL/nil instead of the CGAffineTransformation like this

CGPathMoveToPoint(path, nil, point.x as CGFloat, point.y as CGFloat)

However, I always get an error stating the expression cannot be converted to type NilLiteralConvertible. Do you have any idea why this code does not work? What should be used in Swift insted of the nil argument? I tried using CGAffineTransformidentity instead of nil but it doesn't work either. Thanks!

5

There are 5 answers

2
matt On BEST ANSWER

I suspect your path - you have not shown what it is, so how do we know it is really a CGPath? However, there's also this:

point.x as CGFloat

That is not Swift. You want to say CGFloat(point.x) (and so too for point.y) if it is not a CGFloat already.

0
jwlaughton On

The code below compiles OK for me. Note that it uses a CGMutablePath, not a CGMutablePathRef.

var myMutablePath:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(myMutablePath, nil, CGFloat(1.0), CGFloat(1.0))
0
jwlaughton On

This code fills a red rectangle in the view:

let context:CGContextRef = NSGraphicsContext.currentContext()!.CGContext

var pathPoint:CGPoint = CGPoint()
pathPoint.x = CGFloat(225.0)
pathPoint.y = CGFloat(100.0)

var myMutablePath:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y)
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x + CGFloat(50.0), pathPoint.y  + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y  + CGFloat(50.0))
CGPathAddLineToPoint(myMutablePath, nil, pathPoint.x, pathPoint.y)

CGContextAddPath(context, myMutablePath);

CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0)
CGContextFillPath(context)
0
Jeff On

I took jwlaughton's answer, put it in the context of a UIView subclass, made the layout more flexible, and updated the Core Graphics calls to Swift 4 as indicated by Xcode 9.4 autocomplete.

class RedSquareView: UIView {
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        let length = rect.width < rect.height ? (rect.width * 0.25) : (rect.height * 0.25)
        let start:CGPoint = CGPoint(x: rect.midX - (length/2), y:rect.midY - (length/2))

        let path = CGMutablePath()
        path.move(to: start)
        path.addLine(to: CGPoint(x:start.x + length, y:start.y))
        path.addLine(to: CGPoint(x:start.x + length, y:start.y + length))
        path.addLine(to: CGPoint(x:start.x, y:start.y + length))
        path.addLine(to: CGPoint(x:start.x, y:start.y))

        context.addPath(path)
        context.setFillColor(red:1.0, green:0.0, blue:0.0, alpha:1.0)
        context.fillPath()
    }
}
0
t1ser On

If this comes up with Swift3, just check the new syntax. The Xcode error messages are misleading, this is not a problem just with the nil. The example of the new syntax for your code:

path.move(to: point)