Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ios - Raw value for enum case must be a literal

I have this enum:

enum GestureDirection:UInt {
    case Up =       1 << 0
    case Down =     1 << 1
    case Left =     1 << 2
    case Right =    1 << 3
}

But on every case I get error:

Raw value for enum case must be a literal

I dont get it.

Swift 1.2, Xcode 6.3.2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's because 1 << 0 isn't a literal. You can use a binary literal which is a literal and is allowed there:

enum GestureDirection:UInt {
    case Up =       0b000
    case Down =     0b001
    case Left =     0b010
    case Right =    0b100
}

Enums only support raw-value-literals which are either numeric-literal (numbers) string-literal- (strings) or boolean-literal- (bool) per the language grammar.

Instead as a workaround and still give a good indication of what you're doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...