ios - 我们可以在 Swift 的 Enum 中使用初始值设定项吗?

标签 ios swift enums

我尝试使用 swift 和 enum。我担心的是 swift 中的 Enum 是 Value 数据类型。那么我们可以在这样的结构或类中使用初始值设定项吗?

最佳答案

是的,我们可以:

enum Foo {
    case Bar, Qux

    init?(_ string: String) {
        switch string {
        case "Bar":
            self = .Bar
        case "Qux":
            self = .Qux
        default:
            return nil
        }
    }    
}

let foo = Foo("Bar")!

print(foo) // prints "Bar"

关于ios - 我们可以在 Swift 的 Enum 中使用初始值设定项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456183/

相关文章:

iphone - 解析 XML 时数组被覆盖(Objective-C)

iphone - 应用ARC后仍然面临内存警告

ios - 基于NSFetchedResultsControllerDelegate刷新tableview

python - 如何为枚举对象实现增量函数?

javascript - 类枚举 setter ,约定 javascript

ios - 错误说 "value of type ' FIRDatabaseReference' 没有成员 'observe'“

android - iPhone 5 和 5S 媒体查询级联到三星 Galaxy S4

swift - 观察 swift 5 中的屏幕录制

ios - 如何减少 UIVisualEffectView 上的模糊效果

go - 是否可以通过Golang中的枚举类型反射(reflect)出枚举的变量