Swift enum raw value: not working with CGFloat = -1.0

后端 未结 3 2091
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 14:16

What does this not work?

enum Aspect : CGFloat {
    case Clockwise = 1.0
    case Anticlockwise = -1.0
}

On Anticlockwise line I\'m told t

相关标签:
3条回答
  • 2020-12-21 14:43

    That sounds like a bug. However it seems to work if you omit the decimal part:

    enum Aspect : CGFloat {
        case Clockwise = 1
        case Anticlockwise = -1
    }
    
    0 讨论(0)
  • 2020-12-21 14:46

    The weird thing is that a float with a minus is not a literal, but an expression. So the error message is correct.

    From the Swift programming language:

    Unlike with integer literals, negative floating-point numbers are expressed by applying the unary minus operator (-) to a floating-point literal, as in -42.0. The result is an expression, not a floating-point literal.

    0 讨论(0)
  • 2020-12-21 14:51

    This has been fixed in Swift 1.2 (Xcode 6.3 beta). From the release notes:

    Negative floating-point literals are now accepted as raw values in enums.

    So your code now compiles and works without problems, and you can now also define non-integral negative enumeration values, which was not possible before:

    enum Aspect : CGFloat {
        case Clockwise = 1.0
        case Anticlockwise = -1.0
        case Strange = -1.25
    }
    
    0 讨论(0)
提交回复
热议问题