简体   繁体   English

您如何在快速枚举的情况下为某些情况设置默认值,而为其他情况设置动态值?

[英]How do you cases in swift enumeration that has default values for some cases and dynamic values for others?

Lets take an example of enumeration of FileTypes 让我们以FileTypes枚举为例

  enum FileType {
    case Header
    case Image
    case Swift
    init? (rawValue : String ){
        switch rawValue {
        case "h":
            self = .Header
        case "png" , "jpeg", "jpg":
            self = .Image
        case "swift":
            self = .Swift
        default:
            return nil
        }
    }
}

The above enumeration works fine for the statement 上述列举适用于该语句

let fileType = FileType(rawValue:"jpeg")
let fileType2 = FileType(rawValue:"png")

Both fileType and fileType2 will resolve to .Image enumeration case. fileType和fileType2都将解析为.Image枚举大小写。 But when I access their rawValue, it will contain "Image". 但是,当我访问它们的rawValue时,它将包含“图像”。

How would I achieve to get the actual extension that got resolved to .Image at the first place? 我将如何获得首先解决到.Image的实际扩展名? ie accessing 即访问

fileType.rawValue should result in jpeg fileType.rawValue应该导致jpeg

fileType2.rawValue should result in png . fileType2.rawValue应该产生png

You need to tell explicitly what the values will be. 您需要明确说明这些值是什么。

enum FileType: String {
    case Header = "h"
    case Image = "jpg"
    case Swift = "swift"
}

This way you tell the enum is a string type. 这样,您就可以确定枚举是字符串类型。

EDIT: I forgot about the .Image case. 编辑:我忘记了.Image情况。 I think you cannot have a case that have several different values. 我认为您不能拥有具有多个不同值的案例。 If you'd get a FileType(rawValue: "jpg") then you will get .Image, which is right. 如果获得FileType(rawValue:“ jpg”),则将获得.Image,这是正确的。 But what if you have .Image, how can Swift determine if it is "jpg" or "png"? 但是,如果您具有.Image,Swift如何确定它是“ jpg”还是“ png”呢? That's the wrong approach. 那是错误的方法。

Instead you could extend String, something like this: 相反,您可以扩展String,如下所示:

extension String {
    var extensionIsImage: Bool {
        return self == "jpg" || self == "png"
    }
}

That way you wouldn't lose the value of the extension. 这样,您就不会失去扩展的价值。

EDIT 2: Please note that extension String denotes a Swift extension, not the file extension. 编辑2:请注意, extension String表示Swift扩展名,而不是文件扩展名。

This should give you a better idea of how to handle that type of situation: 这应该使您更好地了解如何处理这种情况:

enum FileType {
    case Header
    case Imge(String)
    case Swift
    init? (fileExt : String ){
        switch fileExt {
        case "h":
            self = .Header
        case "png" , "jpeg", "jpg":
            self = .Imge(fileExt)
        case "swift":
            self = .Swift
        default:
            return nil
        }
    }
}

var fileType = FileType(fileExt:"jpeg")!
var fileType2 = FileType(fileExt:"png")!

switch fileType2 {
case let .Imge(exten):
    print("\(exten)", terminator:"")
default:
    print("Error")
}

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

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