0

Error: Double is not convertible to UInt8 (at the last line of code)

var beweegsnelheid = NSTimeInterval() 
var random3 = CGFloat(arc4random_uniform(100))    
var beweegsnelheidmax = beweegsnelheid * 1.2
var beweegsnelheidmin = beweegsnelheid * 0.8 
   beweegsnelheid = NSTimeInterval(beweegsnelheidmin + (random3/100 * (beweegsnelheidmax - beweegsnelheidmin)))

what am I doing wrong?

sdd
  • 889
  • 8
  • 29

2 Answers2

1

You're trying to multiply a CGFloat by a Double (CGFloat is a typedef for Float).

random3/100 * (beweegsnelheidmax - beweegsnelheidmin)

Unfortunately Swift currently requires explicit type conversions all over the place.

var beweegsnelheid = NSTimeInterval()
var random3 = Float(arc4random_uniform(100))
var beweegsnelheidmax = Float(beweegsnelheid * 1.2)
var beweegsnelheidmin = Float(beweegsnelheid * 0.8)
beweegsnelheid = NSTimeInterval(beweegsnelheidmin + (random3/100 * (beweegsnelheidmax - beweegsnelheidmin)))

Just make sure you use either Float or Double throughout and you shouldn't have a problem.

Gerard Wilkinson
  • 1,512
  • 14
  • 33
0

Would be more obvious if it would complain that CGFloat is not convertible to Double. Swift compiler errors are sometimes really bad.

All of your values are Double, except random3. Make it a Double as well and your code should work.

let random3 = Double(arc4random_uniform(100))    
Gerard Wilkinson
  • 1,512
  • 14
  • 33
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Also, be aware that as your code is written, all of the lines except the random3, are equal to zero. – Magnas Mar 20 '15 at 14:45