你能扩展枚举吗?

时间:2015-10-30 21:07:16

标签: swift enums

我使用枚举来存储这样的字符串值:

    enum Animals: String {
        case descCat = "I has attitude"
        case descDog = "how can I help"
        case descGator = "I will eat you"
        var s: String {
            get {
                return self.rawValue as String
            }
        }
    }

然后我这样访问它们:

print("Dogs be like:" + Animals.descDog.s)

我的问题是我可以像任何其他结构或对象一样扩展枚举,因此我不必将var s: String {}属性添加到每个枚举中吗?

1 个答案:

答案 0 :(得分:71)

You want to add a property to all enums whose raw value is a string? This sounds like a case for constrained protocol extensions!

extension RawRepresentable where RawValue == String {
    var description: String {
        return rawValue
    }
}

This works because all enums with a raw value automatically conform to the RawRepresentable protocol, and said protocol has an associated type RawValue that tells you which type the raw value is.

Now your Animals enum will automatically inherit it:

print(Animals.descCat.description) // -> "I has attitude"

Notice that string enums are themselves already CustomStringConvertible, so they already have a description property (that returns the name of the enum case), and yours doesn't override it:

print(Animals.descCat) // -> "descCat"

If you want your description to override the default, just add a declaration of CustomStringConvertible conformance to your enum:

private enum Animals: String, CustomStringConvertible { /*...*/ }
print(Animals.descCat) // -> "I has attitude"

You can also extend this idea to cover other raw value types. For example:

extension RawRepresentable where RawValue: CustomStringConvertible {
    var description: String {
        return rawValue.description
    }
}

Now, you can get automatic descriptions for enums whose raw value is Int or even a custom type (so long as that type has a description of its own).