Search code examples
swiftswift3cgaffinetransformcgrectcgpath

Nil is not compatible with expected argument type CGAffineTransform


I am trying to draw a path : CGMutablePath but I am facing problem while adding path. Nil is not compatible with expected argument type 'UnsafePointer'CGAffineTransform'

enter image description here

override func draw(_ rect: CGRect) {

            let context = UIGraphicsGetCurrentContext()

            let path = CGMutablePath()
            CGPathAddArc(path, nil, centerX, centerY, radius, -CGFloat(M_PI/2), CGFloat(GLKMathDegreesToRadians(currentAngle)), false)

            context!.addPath(path)
            context!.setStrokeColor(UIColor.blue.cgColor)
            context!.setLineWidth(3)
            context!.strokePath()
       }

Tried few things replace nil with 0, options: nil, options: [ ] none of these worked.

With the help of @vadian answer. Solution:

override func draw(_ rect: CGRect) {

            let context = UIGraphicsGetCurrentContext()!

            let path = CGMutablePath()
            path.addArc(center: CGPoint(x:centerX, y:centerY), radius: radius, startAngle: -(CGFloat.pi / 2), endAngle: CGFloat(GLKMathDegreesToRadians(currentAngle)), clockwise: false)

            context.addPath(path)
            context.setStrokeColor(UIColor.blue.cgColor)
            context.setLineWidth(3)
            context.strokePath()
       }

Solution

  • The signature of the function of CGMutablePath to add an arc in Swift 3 is

    func addArc(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool, transform: CGAffineTransform = default)
    

    in your case (center must be a CGPoint)

    path.addArc(center: CGPoint(x:centerX, y:centerY), 
                radius: radius, 
            startAngle: -(CGFloat.pi / 2), 
              endAngle: CGFloat(GLKMathDegreesToRadians(currentAngle)), 
             clockwise: false)
    

    To avoid the four exclamation marks unwrap the context directly

    let context = UIGraphicsGetCurrentContext()!
    

    and delete all other !.