I have trouble with variable types to declare and use constants.
let MAX_RADIUS_IN_MILE = 100.0
radiusSlider.maximumValue = MAX_RADIUS_IN_MILE
In this case, I get an error - Cannot assign value of type 'Double' to type 'Float'
I don't want to declare a constant with a type definition like this let MAX_RADIUS_IN_MILE: Float = 100.0
but just wanna know another smart way to do make this constant as Float.
Any idea?
For example, C# has a solution for this.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.