设置为属性时,Swift枚举会失去初始化值? [英] Swift enum loses initialized values when set as a property?

查看:106
本文介绍了设置为属性时,Swift枚举会失去初始化值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个解决方案,但是这个问题让我很烦恼,我以为我会分享,以防其他人遇到同样的问题。很想知道为什么会发生这种情况。在下面的代码中,当它是一个局部变量时,我可以在类初始化器中打开枚举。我将枚举值存储到属性中。但是,当我尝试在下面的示例中以不同的方法(名为bar())打开存储的属性(名为foo)时,我收到编译器警告和错误,该成员不被识别。似乎知道foo是一个MyEnum类型,但不知道.ABC,.DEF和.GHI是成员。

I've found a work-around, but this problem is vexing me and I thought I'd share in case anyone else is having the same problem. Would love to know why this is happening. In the code below, I can switch on the enum just fine during the class initializer when it's a local variable. I store the enum value into a property. But when I try to switch on the stored property (named foo) in a different method (named bar()) in the example below - I get compiler warnings and an error that the member(s) are not recognized. It seems to know that foo is a MyEnum type, but doesn't know that .ABC, .DEF, and .GHI are members.


enum MyEnum {
    case ABC, DEF, GHI
}

class MyClass : NSObject {

    var foo : MyEnum!

    convenience init(foo: MyEnum) {

        self.init()

        self.foo = foo

        switch foo {

        case .ABC: println("ABC foo")
        case .DEF: println("DEF foo")
        case .GHI: println("GHI foo")
        default: println("no foo")

        }
    }

    func bar() {

        switch foo {

        case .ABC: println("ABC foo")
        case .DEF: println("DEF foo")
        case .GHI: println("GHI foo")
        default: println("no foo")

        }
    }
}

解决方法是说:

switch foo as MyEnum { }

或在方法中声明一个局部变量,如

or declare a local variable in the method like

let x : MyEnum = foo

switch x {  }

再次,我很高兴找到一个解决方法,但一定要知道这是否是exp如果需要向苹果提交雷达。这是Xcode 6.2,BTW。

Again, glad I found a workaround, but would sure like to know if this is the expected behavior or if a radar needs to be filed with Apple. This is Xcode 6.2, BTW.

推荐答案

属性 foo code> MyEnum ,但 ImplicitlyUnwrappedOptional< MyEnum> aka MyEnum!。与许多其他情况不同, switch 不会隐式解开它。

Property foo is not MyEnum, but ImplicitlyUnwrappedOptional<MyEnum> aka MyEnum!. Unlike many other cases, switch does not implicitly unwrap it.

您必须手动解开它:

    if let foo = foo {
        switch foo {
        case .ABC: println("ABC foo")
        case .DEF: println("DEF foo")
        case .GHI: println("GHI foo")
        default: println("no foo")
        }
    }
    else {
        println("nil foo")
    }



如果您确定 foo 不是! c> code>,

OR force unwrap with ! if you are sure foo is not nil, :

    switch foo! {
    case .ABC: println("ABC foo")
    case .DEF: println("DEF foo")
    case .GHI: println("GHI foo")
    default: println("no foo")
    }

或与 ImplicitlyUnwrappedOptional& Enum> 原样:

    switch foo {
    case .Some(.ABC): println("ABC foo")
    case .Some(.DEF): println("DEF foo")
    case .Some(.GHI): println("GHI foo")
    default: println("no foo")
    }

这篇关于设置为属性时,Swift枚举会失去初始化值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆