iOS – Cannot assign value of type 'Double' to type 'Float'

First, use camelCase for naming in Swift. lowerCamelCase for constants/variables/functions and UpperCamelCase for types (classes, …)

MAX_RADIUS_IN_MILE -> maxRadiusInMile


Now to your problem. Error is clear, you have constant of type Double (if you don’t specify type of decimal number, compiler give it type Double), but assigning maximumValue requires type Float. What now?

One solution if you need your constant to be of type Float is: specify type of your constant as Float

let maxRadiusInMile: Float = 100
let maxRadiusInMile = Float(100)

Anyway, if you need this constant just for assigning one value, use can assign it directly

radiusSlider.maximumValue = 100

If you from some reason need your constant to be of type Double, then you can convert your constant of type Double to Float using designed initializer

let maxRadiusInMile = 100.0
radiusSlider.maximumValue = Float(maxRadiusInMile)

Leave a Comment