Swift enum loses initialized values when set as a property?(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 { }
再次,很高兴我找到了解决方法,但我很想知道这是否是预期的行为,或者是否需要向 Apple 提交雷达.这是 Xcode 6.2,顺便说一句.
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
不是 MyEnum
,而是 ImplicitlyUnwrappedOptional
aka我的枚举!
.与许多其他情况不同,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
不是 nil
,则使用 !
强制解包,:
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
匹配:
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枚举设置为属性时会丢失初始化值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift枚举设置为属性时会丢失初始化值?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01