Search code examples
swiftnull

error "CGPathGetCurrentPoint: no current point."?


I get the error = "CGPathGetCurrentPoint: no current point" with this function:

func getPositionFor(_ node: SKSpriteNode, orPath: UIBezierPath) -> CGPoint {
    
    var thePosition = CGPointZero

    print("here1")   // (1)

    if let theCurrentPoint = orPath.currentPoint as CGPoint? {
        print("\(theCurrentPoint)")   // (3)
        thePosition = theCurrentPoint
    }
    else {
        thePosition = node.position
    }

    print("here2")   // (2)

    return thePosition
    
}   // getPositionFor

With the print("here1") as shown, the console displays:

here1
CGPathGetCurrentPoint: no current point.

=====

With the print("here2") as shown, the console displays:

CGPathGetCurrentPoint: no current point.
here2

With the print("\(theCurrentPoint)"). the console displays:

CGPathGetCurrentPoint: no current point.
(0.0, 0.0)

This very basic troubleshooting tells me that my error is generated somewhere within this conditional binding if clause:

if let theCurrentPoint = orPath.currentPoint as CGPoint?

If there is no current point, the existence of the error would cause just the else part of the code to execute .. and would not display (0.0, 0.0)?

?????


Solution

  • This behaviour is documented in UIBezierPath.currentPoint.

    The value in this property represents the starting point for new line and curve segments. If the path is currently empty, this property contains the value CGPointZero.

    currentPoint presumably calls CGPathGetCurrentPoint using the CGMutablePath that UIBezierPath wraps. This function also documented to return (0, 0) when the path is empty. And as you have observed, it also logs an error message.

    You seem to assume that currentPoint will be nil when the path is empty. This is not true - the type of the property is not an optional type. You should check isEmpty instead.

    let thePosition = orPath.isEmpty ? node.position : orPath.currentPoint