在Swift 2中编写一个简单的枚举,就像在Java中一样

在Swift 2中编写一个简单的枚举,就像在Java中一样,swift,enums,case,Swift,Enums,Case,在Java中,我可以创建一个接受整数的枚举,如下所示: public enum MyEnum { FOO(1), BAR(4), BAZ(8); private int value; MyEnum(int value) { this.value = value; } public int getValue() { return value; } } 如何在Swift 2.0版中执行相同操作?我

在Java中,我可以创建一个接受整数的枚举,如下所示:

public
enum MyEnum {
    FOO(1), BAR(4), BAZ(8);

    private
    int value;

    MyEnum(int value) {
        this.value = value;
    }

    public
    int getValue() {
        return value;
    }
}
如何在Swift 2.0版中执行相同操作?我的猜测是:

public
enum MyEnum {
    case FOO(1)
    case BAR(4)
    case BAZ(8)

    public
    let value: Int

    init(value: Int) {
        self.value = value
    }
}
但试图编译此文件会出现以下错误:

Expected type
Expected ',' separator
Expected type
Expected ',' separator
Expected type
Expected ',' separator
Enums may not contain stored properties
行case FOO1上的前两个,case BAR4上的第二个,case BAZ8上的第三个,以及行let值上的最后一个错误:Int

如何修复此Swift代码以创建与Java中的枚举等效的枚举

非常感谢

 public
  enum MyEnum: Int {
    case FOO = 1
    case BAR = 4
    case BAZ = 8
  }

  var foo = MyEnum(rawValue: 4)
  var rawValue = foo?.rawValue
在Swift中,不需要底层值的构造函数/getter。它们作为rawValue包含在枚举类型中,rawValue是枚举从中继承的类型

另外,请注意,接受rawValue的构造函数是可失败的,即返回一个可选值,因为可以传入一个不映射到MyEnum的rawValue

在Swift中,不需要底层值的构造函数/getter。它们作为rawValue包含在枚举类型中,rawValue是枚举从中继承的类型


另外,请注意,接受rawValue的构造函数是可失败的,即返回一个可选的,因为可以传入一个不映射到MyEnum的rawValue

public enum MyEnum: Int { // You need to declare the raw type
    case FOO = 1 // use "= x" rather than "(x)"
    case BAR = 4
    case BAZ = 8

    //public let value: Int // Enums may not contain stored properties
    //... but can contain computed values
    var value: Int {
        get {
            return self.rawValue
        }
    }

    init?(value: Int) { // Fails if not 1, 4 or 8, hence "init?"
        //self.value = value // Now meaningless
        self.init(rawValue: value)
    }
}
或者,您可以跳过其中的大部分内容,使用内置的Enum init&properties

public enum MyEnum: Int { // You need to declare the raw type
    case FOO = 1 // use "= x" rather than "(x)"
    case BAR = 4
    case BAZ = 8
}

let myEnum = MyEnum(rawValue: 4) // BAR
let four = myEnum?.rawValue // 4
let another = MyEnum(rawValue: 5) // nil

试试这个,作为直接翻译

public enum MyEnum: Int { // You need to declare the raw type
    case FOO = 1 // use "= x" rather than "(x)"
    case BAR = 4
    case BAZ = 8

    //public let value: Int // Enums may not contain stored properties
    //... but can contain computed values
    var value: Int {
        get {
            return self.rawValue
        }
    }

    init?(value: Int) { // Fails if not 1, 4 or 8, hence "init?"
        //self.value = value // Now meaningless
        self.init(rawValue: value)
    }
}
或者,您可以跳过其中的大部分内容,使用内置的Enum init&properties

public enum MyEnum: Int { // You need to declare the raw type
    case FOO = 1 // use "= x" rather than "(x)"
    case BAR = 4
    case BAZ = 8
}

let myEnum = MyEnum(rawValue: 4) // BAR
let four = myEnum?.rawValue // 4
let another = MyEnum(rawValue: 5) // nil

您的Java示例使用具有整数值的枚举常量,这些整数值是 二的幂。因此,我假设您的意图是定义 类型,它不仅可以表示这三个常量,还可以表示任意值 整数,例如12表示BAR+BAZ,即位集

在Swift中,您可以使用符合 选择设置类型。以下是一个例子:

struct FileAccess: OptionSetType {
    let rawValue: Int

    static let Read    = FileAccess(rawValue: 1 << 2)
    static let Write   = FileAccess(rawValue: 1 << 1)
    static let Execute = FileAccess(rawValue: 1 << 0)
}
或者从一个组合中:

let a2 = FileAccess([.Read, .Write])
print(a2.rawValue) // 6
或从任意整数:

let a3 = FileAccess(rawValue: 5)
print(a3.rawValue) // 5
与enum不同,这不会返回可选值,也不会失败

OptionSetType提供了一个类似集合的界面,可以使用 要创建新值,请执行以下操作:

let r = FileAccess.Read
let w = FileAccess.Write
let rw = r.union(w)
或测试值:

if a1.contains(.Read) {
    print("readable")
}

if a2.isSupersetOf([.Read, .Write]) {
    print("read-write")
}

您的Java示例使用具有整数值的枚举常量,这些整数值是 二的幂。因此,我假设您的意图是定义 类型,它不仅可以表示这三个常量,还可以表示任意值 整数,例如12表示BAR+BAZ,即位集

在Swift中,您可以使用符合 选择设置类型。以下是一个例子:

struct FileAccess: OptionSetType {
    let rawValue: Int

    static let Read    = FileAccess(rawValue: 1 << 2)
    static let Write   = FileAccess(rawValue: 1 << 1)
    static let Execute = FileAccess(rawValue: 1 << 0)
}
或者从一个组合中:

let a2 = FileAccess([.Read, .Write])
print(a2.rawValue) // 6
或从任意整数:

let a3 = FileAccess(rawValue: 5)
print(a3.rawValue) // 5
与enum不同,这不会返回可选值,也不会失败

OptionSetType提供了一个类似集合的界面,可以使用 要创建新值,请执行以下操作:

let r = FileAccess.Read
let w = FileAccess.Write
let rw = r.union(w)
或测试值:

if a1.contains(.Read) {
    print("readable")
}

if a2.isSupersetOf([.Read, .Write]) {
    print("read-write")
}