3

I was adapting an Objective-C Library (STKSpinnerView) to Swift and I can't solve this error:

func getRadius() -> CGFloat {
    var r : CGRect = CGRectInset(self.bounds, self.wellThickness/2.0, self.wellThickness/2.0)
    var w : CGFloat = r.size.width
    var h : CGFloat = r.size.height

    if w > h {
        return h/2.0
    }else{
        return w/2.0
    }
}

override func layoutSubviews()  {
    super.layoutSubviews()

    var bounds : CGRect = self.bounds
    var wt : CGFloat = self.wellThickness
    var outer : CGRect = CGRectInset(self.bounds, wt/2.0, wt/2.0)
    var inner : CGRect = CGRectInset(self.bounds, wt, wt)

    var innerPath : UIBezierPath = UIBezierPath(ovalInRect: inner)
    var arcCenter = CGPointMake(CGRectGetMidX(outer), CGRectGetMidY(outer))
    var radius = self.getRadius()
    var startAngle = -(CGFloat(M_PI_2))
    var endAngle = (2.0 * M_PI - M_PI_2)
    // (Next line) ERROR: Extra argument 'radius' in call
    var outerPath : UIBezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true) 

}

I don't understand why 'radius' is an extra argument if I use the method like Apple say... Do you know how to solve it? Thanks!

rulilg
  • 1,604
  • 4
  • 15
  • 19

1 Answers1

11

The error message is very misleading. The real problem is that the endAngle: parameter of UIBezierPath(...) has the type CGFloat, but you are passing a Double argument. Casting the value explicitly helps:

var endAngle = CGFloat(2.0 * M_PI - M_PI_2)

The problem occurs when compiling for the 32-bit architecture, where CGFloat is defined as Float and not as a Double.

See also https://github.com/ksm/SwiftInFlux:

What is happening here is that CGFloat is a typealias for either Float or Double depending on whether you're building for 32 or 64-bits. This is exactly how Objective-C works, but is problematic in Swift because Swift doesn't allow implicit conversions.

We're aware of this problem and consider it to be serious: we are evaluating several different solutions right now and will roll one out in a later beta. As you notice, you can cope with this today by casting to Double. This is inelegant but effective :-)

-- Chris Lattner

Sources: https://devforums.apple.com/message/998222#998222

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Great! Some insight by Chris Lattner. Missed his post and I really appreciate your quote! – Klaas Sep 01 '14 at 12:35
  • 1
    @FrankSchmitt: I have verified it again with Xcode 6.1.1, and with my proposed change the code does compile. – Martin R Dec 04 '14 at 19:24
  • @MartinR You're right. I was assigning to a variable of type CGPath, which caused an identical error. Adding a trailing `.CGPath` fixed it. – Frank Schmitt Dec 04 '14 at 19:45