枚举的rawValue属性无法识别 [英] enum's rawValue property not recognized

查看:237
本文介绍了枚举的rawValue属性无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 枚举Rank:String 
{
case One =One,Two =Two

init(rawValue:String)
{
self.rawValue = rawValue
}
}

我想覆盖init,以便可以使用它的rawValue作为参数初始化枚举。但是我收到一个错误:





但是根据 Apple的Swift指南我的代码应该是正确的。

解决方案

马丁的答案是完全正确的



这是一个不同的视图,可以直接回答您的问题。



在Xcode 6.0中,枚举没有一个 rawValue 属性。在Xcode 6.1中添加了 rawValue ,但注意它是一个只读计算属性,因此您不能在Xcode 6.1中分配



在Xcode 6.1中,没有必要实现一个采用 rawValue 的初始化程序,因为已经提供本来由语言。如果您试图在Xcode 6.0中模仿这种行为,那么您可以尝试以下方式:

 枚举Rank:String 
{
case One =One,Two =Two

init(rawValue:String)
{
self = Rank.fromRaw(rawValue)
}
}

但这个问题是 fromRaw 返回一个可选的枚举值,因为 rawValue 字符串可能对应于任何枚举值。



那么你现在在做什么呢?您可以添加一个来强制解开该值:

  self = Rank.fromRaw(rawValue)! 

但是如果您尝试创建一个无效的原始值的枚举,那么这将会崩溃。



您可以将其中一个枚举值作为默认值使用,并使用 nil coalescing operator ?? 以安全地解开它:

  self = Rank.fromRaw(rawValue)?一个

这将避免崩溃,但可能会导致程序的意外行为。



您在Xcode 6.0中不能执行的操作是使$ init 返回一个可选的值。 Xcode 6.1中添加了此功能,正是这种新功能允许他们从Xcode 6.0中的函数将 fromRaw()更改为Xcode 6.1中的可选初始化程序。 / p>

I'm using Xcode 6's playground to try out enums in Swift:

enum Rank: String
{
    case One = "One", Two="Two"

    init(rawValue : String)
    {
        self.rawValue = rawValue
    }
}

I want to override init so that the enum can be initialized using it's rawValue as argument. But I get an error:

But according to the Apple's Swift guide my code should be correct.

解决方案

Martin's answer is completely right.

Here is a different view that more directly answers your question.

In Xcode 6.0, an enum doesn't have a rawValue property. rawValue was added in Xcode 6.1 but note that it is a read-only computed property, so you can't assign to it in Xcode 6.1 either.

In Xcode 6.1, it is unnecessary to implement an initializer that takes a rawValue because that has already been provided natively by the language. If you were trying to imitate that behavior in Xcode 6.0, then you might try something like:

enum Rank: String
{
    case One = "One", Two="Two"

    init(rawValue : String)
    {
        self = Rank.fromRaw(rawValue)
    }
}

but the problem with this is that fromRaw returns an optional enum value because the rawValue string might correspond to any enum value.

So what do you do at this point? You could add a ! to force unwrap the value:

self = Rank.fromRaw(rawValue)!

but this would crash if you tried to create an enum with an invalid raw value.

You could treat one of the enum values as a default and use the nil coalescing operator ?? to safely unwrap it:

self = Rank.fromRaw(rawValue) ?? One

which would avoid a crash, but would probably lead to unexpected behavior on the part of your program.

What you can't do in Xcode 6.0 is have the init return an optional value. This capability was added in Xcode 6.1 and it was exactly this new capability that allowed them to change fromRaw() from a function in Xcode 6.0 to an optional initializer in Xcode 6.1.

这篇关于枚举的rawValue属性无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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