1

Usually this works fine for me, but for some reason I am getting the error "Cannot assign a value of type 'CGFloat' to a value of type 'Int'" right now. I am simply trying to take my randomly generated integer and turn it into a CGFloat. However, it appears the code UInt32(self.view!.frame.width - dotRadius * 2) is generating an error. Here is the code:

    x = CGFloat(arc4random_uniform(UInt32(self.view!.frame.width - dotRadius * 2))) + dotRadius

Here is where I declare the dotRadius variable:

var dotRadius: CGFloat = 0

Anyone know why I'm not allowed to convert that? Any help is appreciated. Thanks!

Kendel
  • 1,698
  • 2
  • 17
  • 33
  • The error-message is not saying that you can't convert an integer to a CGFloat, but rather the reverse. So it's presumably complaining about the `UInt32(...)` part (not the `CGFloat(...)` part, which is fine). – ruakh Jun 28 '15 at 23:00
  • how is dotRadius declared? – the_pantless_coder Jun 28 '15 at 23:00
  • @ruakh you are correct. How would I go about converting that part then? the_pantless_coder it is a CGFloat I declared earlier – Kendel Jun 28 '15 at 23:02
  • maybe I am missing something, but I dont get any errors with " var dotRadus:CGFloat = 666 var x = CGFloat(arc4random_uniform(UInt32(self.view!.frame.width - dotRadus * 2))) + dotRadus" – the_pantless_coder Jun 28 '15 at 23:06
  • My guess is that `dotRadius` is an integer type when it should be `CGFloat`. – Arkku Jun 28 '15 at 23:08
  • @Arrku, thats what I am thinking. I also tried " let dotRadus:Int = 666 let x = CGFloat(arc4random_uniform(UInt32(self.view!.frame.width - CGFloat(dotRadus) * 2))) + CGFloat(dotRadus) " with no errors – the_pantless_coder Jun 28 '15 at 23:09
  • @the_paintless_coder I put in the original question how I declare the dotRadius variable. It is already a CGFloat. – Kendel Jun 28 '15 at 23:19

1 Answers1

3

The CGFloat(...) part is fine; the error-message isn't saying that you can't explicitly convert an integer to a CGFloat, but rather, that you can't implicitly convert from a CGFloat to an Int. More specifically, I think it's complaining because of the final assignment x = ...; the part on the right is a CGFloat, and I'm guessing that x is an Int?

If so, then you should be able to fix this by adding an explicit cast from CGFloat to Int:

x = Int(CGFloat(arc4random_uniform(UInt32(self.view!.frame.width - dotRadius * 2))) + dotRadius)

This Stack Overflow answer suggests that you also include an explicit floor or round (from the math libraries) before casting to Int, to make it clear what kind of rounding you expect.

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Tried with that exact code and it still isn't working. I also tried using a floor function, with no effect. Strange part is I was using code VERY similar to this earlier and it worked fine... – Kendel Jun 28 '15 at 23:17
  • @Kendel: Wait, I'm being silly. What it's really complaining about is probably the assignment at the very end; `x` is probably declared as `Int`? – ruakh Jun 28 '15 at 23:25
  • Wow. Yep that is correct. Forgot that x is declared as `Int` Thanks for the help! – Kendel Jun 28 '15 at 23:28