转移案件的初始值应该是一个字面

我有这个列表:


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


但在每种情况下,我犯了一个错误:

转移案件的初始值应该是一个字面

我不明白。

Swift 1.2, Xcode 6.3.2
已邀请:

龙天

赞同来自:

这是因为
1 << 0

它不是文字。 您可以使用二进制文字,这是一个文字,允许在那里:


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


Enums 仅支持
raw-value-literal

s, 哪些是
numeric-literal

/数字/
string-literal­

/线时间/, 或者
boolean-literal­

/bool/ 为了
https://developer.apple.com/li ... ID351
.

相反,作为一个解决方法,仍然对自己在做什么来说。

窦买办

赞同来自:

对于不互斥的属性,我还会推荐
使用
struct

基于
RawOptionSetType

, 如图所示 @Vincent.
其中一个优点是您免费获得所有位操作。

以下是一个完整的工作示例:


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/ }
}


使用:


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

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

// Add an option:
direction |= .Left

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


更新 Swift 2:

以。。。开始 Swift 2, 它可以更容易完成
在新协议的帮助下
OptionSetType

, 这表达了
像一套一样的界面 /另请参阅最近添加了问题的答案
https://coderoad.ru/24066170/
/.

我们只需要确定基本存储类型和预定值:


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/
}


使用:


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

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

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

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

君笑尘

赞同来自:

Swift 2.2 版本 :
在我的情况下,我需要转换字符串列出的值以用于本地化行。 因此,我将此方法添加到您的列表中。


enum DisplayCellTitle: String {
case Clear

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


然后使用它 :


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


在哪里 cellTitle 转移 - 这只是其中一个列表值之一。 CellTitle

八刀丁二

赞同来自:

看起来你需要对你的弯曲支持 enums, 但如果你想翻译
NS_OPTIONS

Objective-C 在 Swift, 它不会由上市代表 Swift, 和
struct

它是遗传的
RawOptionSetType

.

如果您需要一个示例或说明,您可以看到此
http://nshipster.com/rawoptionsettype/
它可以用这样的东西完成 :


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 }
}


尊敬,

要回复问题请先登录注册