"Raw value for enum case is not unique" for Swift enum with Float raw values -


According to

, I should be able to create a swift enum with "strings, characters, or raw values ​​of any integer Or floating point number type "but when I try:

  enum battery voltage: float {case v3v7 = 3.7 case v5v0 = 5.0 case v7v4 = 7.4 case v11v1 = 11.1 case v12v0 = 12.0}  

... I get a compilation error:

  The raw value is not unique for the inf  

V7v4 It's fine on the line that one comment with the comment out but ah, it looks unique to me if I make the value 7.41, or 7.3 or something, then it compiles exactly what's going on? Swift bug?

It definitely says that you call , but its use Do not temporarily point values ​​(and especially float ) where you are going to need to compare parallelism - the exact just is not guaranteed enough and always double Use unless you need to use float for compatibility reasons.

In this situation, it seems that the problem is happening (a) the third case is 2x the first case, and (b) some other factors which I do not know 3.3 / 6.6 , 3.4 / 6.8 , and 3.6 / 7.2 also gave me the problem, but I was not 3.5 / 7.0 However, I I can show this by changing the previous code to 22.2 (2x 11.1 ).

Here's a solution - Provide a specific int based calculation, and a double value property:

  Enum battery voltage: int {case v3v7 case v5v0 case v7v4 case v11v1 case v12v0 var double value: double {switch self {case. V3v7: return 3.7 cases. V5v0: Return 5.0 case. V7v4: Return 7.4 matter. V11v1: Return 11.1 case V12v0: return 12.0}}}  

If you are based on int , then you can take advantage of it.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -