ios 枚举大小写的原始值必须是文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30325058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:00:57  来源:igfitidea点击:

Raw value for enum case must be a literal

iosswift

提问by Arbitur

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

斯威夫特 1.2,Xcode 6.3.2

采纳答案by Benjamin Gruenbaum

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

那是因为1 << 0不是文字。您可以使用一个二进制文字,它是一个文字并且在那里被允许:

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.

枚举仅支持根据语言语法raw-value-literalnumeric-literal(numbers) string-literal-(strings) 或boolean-literal-(bool) 的s 。

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

相反,作为一种解决方法,仍然可以很好地说明您在做什么。

回答by Martin R

For attributes which are not mutually exclusive I would also recommend to use a structbased on RawOptionSetTypeas @Vincent suggested. One advantage is that you get all bit operations for free.

对于不相互排斥的属性,我还建议使用@Vincent 建议的struct基于RawOptionSetType。一个优势是您可以免费获得所有位操作。

Here is a full working example:

这是一个完整的工作示例:

struct GestureDirection : RawOptionSetType {
    let rawValue : UInt8
    init(rawValue: UInt8) {
        self.rawValue = rawValue
    }

    init(nilLiteral: ()) {
        self.rawValue = 0
    }
    static var allZeros: GestureDirection { return self(rawValue: 0) }

    static var Top:   GestureDirection { return self(rawValue: 1 << 0) }
    static var Down:  GestureDirection { return self(rawValue: 1 << 1) }
    static var Left:  GestureDirection { return self(rawValue: 1 << 2) }
    static var Right: GestureDirection { return self(rawValue: 1 << 3) }
}

Usage:

用法:

// Initialize:
var direction : GestureDirection = .Top | .Right

// Test:
if (direction & .Top) != nil {
   // ...
}

// Add an option:
direction |= .Left

// Remove an option:
direction &= ~(.Right)


Update for Swift 2:As of Swift 2, this can be simpler done with the new OptionSetTypeprotocol, which offers a set-like interface (see also the recently added answers to How to create NS_OPTIONS-style bitmask enumerations in Swift?).

Swift 2 的更新:从 Swift 2 开始,这可以通过新OptionSetType协议更简单地完成,它提供了一个类似集合的接口(另请参阅最近添加的如何在 Swift 中创建 NS_OPTIONS 样式位掩码枚举的答案)。

We just have to define the underlying storage type and the pre-defined values:

我们只需要定义底层存储类型和预定义值:

struct GestureDirection : OptionSetType {
    let rawValue : UInt8

    static let Top   = GestureDirection(rawValue: 1 << 0)
    static let Down  = GestureDirection(rawValue: 1 << 1)
    static let Left  = GestureDirection(rawValue: 1 << 2)
    static let Right = GestureDirection(rawValue: 1 << 3)
}

Usage:

用法:

// Initialize:
var direction : GestureDirection = [ .Top, .Right ]

// Test:
if direction.contains(.Top) {
    // ...
}

// Add an option:
direction.insert(.Left)

// Remove an option:
direction.remove(.Right)

回答by ioopl

Swift 2.2 Version : In my case I needed to Convert the String Enum Values to be used in Localisable Strings. So added this method inside my enum.

Swift 2.2 版本:在我的情况下,我需要转换字符串枚举值以在可本地化的字符串中使用。所以在我的枚举中添加了这个方法。

    enum DisplayCellTitle: String {
    case Clear 

    func labelTitle() -> String {
        switch self {
        case .Clear:
            return "LBL_CLEAR".localizedWithComment("Clear")
        }
    }
}

And then Use it like so :

然后像这样使用它:

        // Get the localised value of the Cell Label Title
    let lblTitle = DisplayCellTitle.labelTitle(cellTitle)()

where cellTitle passed in is just one of these CellTitle Enum Values

传入的 cellTitle 只是这些 CellTitle 枚举值之一

回答by Vincent Saluzzo

You seems to want a bitwise support for your enums, but if you regards a translation of NS_OPTIONSObjective-C in Swift, that's not represented by a Swift Enum but a structinherit from RawOptionSetType.

您似乎想要对您的枚举按位支持,但是如果您认为NS_OPTIONSSwift中Objective-C的翻译,那不是由 Swift Enum 表示,而是由struct继承自RawOptionSetType.

If you need example or instructions, you can look at this NSHipster article

如果您需要示例或说明,可以查看这篇NSHipster 文章

That's could be done with something like this :

这可以通过这样的事情来完成:

struct UIViewAutoresizing : RawOptionSetType {
    init(_ value: UInt)
    var value: UInt
    static var None: UIViewAutoresizing { get }
    static var FlexibleLeftMargin: UIViewAutoresizing { get }
    static var FlexibleWidth: UIViewAutoresizing { get }
    static var FlexibleRightMargin: UIViewAutoresizing { get }
    static var FlexibleTopMargin: UIViewAutoresizing { get }
    static var FlexibleHeight: UIViewAutoresizing { get }
    static var FlexibleBottomMargin: UIViewAutoresizing { get }
}

Regards,

问候,