简体   繁体   English

Swift:无法打开枚举关联值

[英]Swift: Unable to switch on enum associated value

I'm converting an old Objective C app into SWIFT for learning purposes, and I've stumbled upon a strange problem when trying to provide a switch statement on an enum. 我出于学习目的将旧的Objective C应用程序转换为SWIFT,当尝试在枚举中提供switch语句时,我偶然发现了一个奇怪的问题。

The code looks like this: 代码如下:

switch entry.mood {
    case let Mood.THDiaryMoodGood:
        self.moodImageView.image = UIImage(named: "icn_happy")
    case let Mood.THDiaryMoodAverage:
         self.moodImageView.image = UIImage(named: "icn_average")
    case let Mood.THDiaryMoodBad:
        self.moodImageView.image = UIImage(named: "icn_bad")
    default:
        self.moodImageView.image = UIImage(named: "icn_happy")
}

Where Mood is: 心情在哪里:

enum Mood: Int16 {
    case THDiaryMoodGood = 0
    case THDiaryMoodAverage = 1
    case THDiaryMoodBad = 2
}

And the representation in mood is stored in a CoreData entity named mood with the type Integer 16 . 而在情绪的表示保存在名为CoreData实体mood与类型Integer 16

My casts directly matched each-other, however when I try and use the switch statement provided above I get the error: Enum case pattern cannot match values of the non-enum type Int16 . 我的类型转换彼此直接匹配,但是当我尝试使用上面提供的switch语句时,我得到了错误: Enum case pattern cannot match values of the non-enum type Int16

I'm rather confused as to why I'm receiving this error, from my understanding the process should be evaluated like this: 对于我为什么会收到此错误,我感到很困惑,从我的理解来看,应该这样评估流程:

entry.mood = 1

switch(1) {
   // Int16: 0 returned from enum - would evaluate false and fall through to case B
   case Mood.THDiaryMoodGood:   
        self.mood.image = ...   
   // Int16: 1 returned from enum - would evaluate true and set the icon
   case Mood.THDiaryMoodAverage:
        self.mood.image = ...
   // Other cases not evaluated as we returned true...
}

Is my thought process or logic flawed here? 我的思考过程或逻辑是否存在缺陷? I'm very confused...any help would be greatly appreciated, thanks! 我很困惑...任何帮助将不胜感激,谢谢!

The problem is that you're passing an Int16 value to the switch. 问题在于您要将Int16值传递给交换机。 You're setting entry.mood, an Int16, to the raw value 1, but the switch wants your Mood type. 您正在将Int16的entry.mood设置为原始值1,但是交换机需要您的Mood类型。 So you have a type mismatch. 所以你有一个类型不匹配。

You can solve it by turning the value into a Mood: 您可以通过将值转换为心情来解决它:

switch Mood(rawValue: entry.mood)! {

Swift enums are not the same as Objective-C enums. Swift枚举与Objective-C枚举不同。 In Objective-c, the enum is the backing value , referred to by a different name. 在Objective-c中, 枚举是备用值 ,由另一个名称引用。 In swift, the enum is it's own type, and the value is an associated value (you don't actually need a backing value if you don't want one, in your case it makes sense though). 快速地,枚举是它自己的类型,并且值是一个关联的值(如果您不想要某个值,则实际上不需要后备值,但在您的情况下,这很有意义)。

Have a read of this, it explains things nicely. 读一读,它很好地解释了事情。 https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM