Swift 枚举继承 [英] Swift enum inheritance

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

问题描述

你能在 Swift 中继承 enum 吗?枚举继承需要注意哪些规则?

Can you inherit enum in Swift? What are the rules that one should be aware of with regards to enum inheritance?

以下测试代码:

enum TemperatureUnit: Int {
    case Kelvin, Celcius, Farenheit
}

enum TemperatureSubunit : Temperature {  
}

生成

error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable'

推荐答案

在 Swift 语言中,我们有 Structs、Enum 和 Classes.Struct 和 Enum 是通过复制传递的,而类是通过引用传递的.只有类支持继承,枚举和结构不支持.

In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.

所以要回答您的问题,您不能继承 Enum(和 Struct 类型).看看这里:

So to answer your question, you can't have inheritance with Enum (and Struct types). Have a look here:

stackOverflow 差异类与结构

这篇关于Swift 枚举继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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