He definitely says that you can, but donβt use floating point values ββ(and especially Float ), where you will need to compare equality - accuracy is simply not guaranteed. And always use Double unless you need to use the Float parameter for compatibility reasons.
In this case, it seems that he has problems, because (a) the third case is equal to 2x of the first case, and (b) some other factor that I do not know. Using 3.3/6.6 , 3.4/6.8 and 3.6/7.2 also gave me a problem, but 3.5/7.0 did not. However, I could make it appear by changing the last case to 22.2 (2x 11.1 ).
Here's a workaround - use typical Int based numbering and provide the doubleValue property:
enum BatteryVoltage: Int { case v3v7 case v5v0 case v7v4 case v11v1 case v12v0 var doubleValue: Double { switch self { case .v3v7: return 3.7 case .v5v0: return 5.0 case .v7v4: return 7.4 case .v11v1: return 11.1 case .v12v0: return 12.0 } } }
There are a few additional additional enumeration options that you can use if they are Int based.
Nate cook
source share