你可以扩展一个枚举吗? [英] Can you extend an enum?

查看:72
本文介绍了你可以扩展一个枚举吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 枚举动物:字符串{
case descCat =我的态度是
case descDog =我如何提供帮助
case descGator =我会吃掉你的
var s:字符串{
get {
return self.rawValue as String
}
}
}

然后我像这样访问它们:

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

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

解决方案

您要添加一个所有原始值是字符串的枚举的属性?

 扩展RawRepresentable,其中RawValue == String {
var description:String {
return rawValue
}
}

具有原始值的枚举自动符合 RawRepresentable 协议,并且该协议具有关联的类型 RawValue ,该类型告诉您哪个



现在您的 Animals 枚举将自动继承它:

  print(Animals.descCat.description)//-> 我有态度 

请注意,字符串枚举本身已经是 CustomStringConvertible ,因此它们已经具有 description 属性(该属性返回枚举大小写的名称),而您的属性不会覆盖它:

  print(Animals.descCat)//->  descCat 

如果您想要您的描述要覆盖默认值,只需向您的枚举添加 CustomStringConvertible 一致性声明:

  private枚举动物:String,CustomStringConvertible {/*...*/} 
print(Animals.descCat)//-> 我有态度






您也可以扩展此内容涵盖其他原始价值类型的想法。例如:

 扩展RawRepresentable其中RawValue:CustomStringConvertible {
var描述:字符串{
返回rawValue。 description
}
}

现在,您可以自动获取其枚举的描述原始值是 Int 甚至是自定义类型(只要该类型具有自己的描述)。 / p>

I use enums to store string values like this:

    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
            }
        }
    }

Then I access them like this:

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

My question is can I extend enums like any other struct or object so I don't have to add the var s: String {} property to each enum?

解决方案

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).

这篇关于你可以扩展一个枚举吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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