Need help converting code to Swift for circle max radius


I am toward the end of chapter 4 where you are supposed to determine the maximum radius to start drawing the concentric circles.

For the Objective C code:

I have tried the following Swift code:

but I get the error “Could not find an overload for ‘/’ that accepts the supplied arguments.” I thought this might be due to type differences in the expression, so tried converting the hypot to a float:

but I still get the same error. Any ideas how to fix?


Have you tried the following:

var maxRadius = Float (hypot (bounds.size.width, bounds.size.height)) / Float (2.0)


var maxRadius = Double (hypot (bounds.size.width, bounds.size.height)) / Double (2.0)

Switft is very fussy about the types of operands; also, doesn’t do implicit (behind-the-scenes) conversions.

As an aside: if it is not going to change, better to make the maxRadius a constant:

let maxRadius = Float (hypot (bounds.size.width, bounds.size.height)) / Float (2.0)


let maxRadius = Double (hypot (bounds.size.width, bounds.size.height)) / Double (2.0)


Thanks for the reply. I’ve tried both of those options and continue to get the same error.

Interestingly, it seems the first part of the expression is the problem. When I just try to set the first part of the expression to a variable:

I get the error “Cannot convert the expressions’s type ‘CDouble’ to type’$T8’”

If I try to set this variable to a double:

I get the error:

I get the error “Cannot convert the expressions’s type ‘CDouble’ to type’CDouble’”

If I try to set the variable to Float:

I get the error “’‘NSNumber’ is not a subtype of ‘Float’”


I was able to get around this by simply mathematically calculating the hypotenuse without hypot:

Still have no idea what the problem was with the other code.


This might be caused by something to do with the integration of Swift with the existing Objective-C math library.

If were you, I would not desert Objective-C :slight_smile: until Swift reaches maturity and becomes fully integrated with the existing frameworks and libraries.


Thanks. Probably good advice :smiley:

But for a novice like me, I’ve noticed that I’ve made way more progress in this book and been more engaged n the learning by using Swift than I did with Objective C. Swift is just so much easier to work with (at least to me). At the same time, by converting the book code to Swift, I feel like I am strengthening my learning of both languages. If there comes a point in the book where I cannot proceed due to likely Swift incompatibility, I’ll just write that file in Obj C.


I think the problem is that the width and height of the bounds are CGFloat, but hypot requires CDouble. So you can fix this by using


I tried the line as Bracketed suggested and that line compiles, but then gives an error later if trying to use maxRadius as a CGFloat. (It gets inferred as a Float, not a CGFloat and to redo Chapter 4 in Swift it seems that it wants to be a CGFloat.)

I got around this like this:

let maxRadius:CGFloat = CGFloat(hypot(Double(bounds.width), Double(bounds.height)) / 2.0)
It works, but it looks so clumsy. Is there a better way to write this?


Here’s what I did. Maybe I’m overlooking something, but Swift seems to have inferred the types just fine without any explicit type casting: