有没有办法在Swift 2中做这样的事情?
enum Placement: Int, OptionSetType {
case
Left = 1 << 0,
Right = 1 << 1,
Center = 1 << 2,
Top = 1 << 3,
Bottom = 1 << 4,
Middle = 1 << 5
;
....
}
实际问题是编译器不够聪明,不能看到这些值是常量,但比结果更具可读性。
那么,是否有一些允许这种声明的语法糖?
答案 0 :(得分:2)
正如@Martin R所说,你需要结构。
struct Placement: OptionSetType {
let rawValue: Int
init(rawValue: Int) {
self.rawValue = rawValue
}
static let Left = Placement(rawValue: 1 << 0)
static let Right = Placement(rawValue: 1 << 1)
static let Center = Placement(rawValue: 1 << 2)
static let Top = Placement(rawValue: 1 << 3)
}