Swift枚举继承

Boo*_*oon 53 inheritance enums ios swift rawrepresentable

你能在Swift中继承enum吗?关于枚举继承,应该注意哪些规则?

以下测试代码:

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

enum TemperatureSubunit : Temperature {  
}
Run Code Online (Sandbox Code Playgroud)

生成

error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable'
Run Code Online (Sandbox Code Playgroud)

Kor*_*pel 65

在斯威夫特的语言,我们的Structs,枚举和类.结构和枚举被复制过去了,但类是按引用传递.只有Classes支持继承,Enum和Struct不支持继承.

因此,要回答你的问题,你不能有枚举(和结构类型)的继承.看看这里:

stackOverflow差异类vs结构

  • 惊人的失败,但就这样吧。谢谢你的信息。 (2认同)

Lui*_*uis 58

正如Korpel已经回答的那样,目前Enums没有支持实际的继承.所以不可能有一个Enum扩展并继承另一个枚举的情况.

但是,我想补充完成,Enums确实支持协议,并且与Swift 2中引入的协议扩展和新的面向协议的编程方法(参见此视频)一起,可以实现类似于继承的东西.这是一种我经常使用的技术UITableViewController:由枚举驱动,指定表的各个部分以及每个部分中的行,以及添加一些有用的行为.请参阅以下示例代码:

import UIKit

protocol TableSection {
    static var rows: [Self] { get }

    var title: String { get }

    var mandatoryField: Bool { get }
}

extension TableSection {
    var mandatoryTitle: String {
        if mandatoryField {
            return "\(title)*"
        } else {
            return title
        }
    }
}

enum RegisterTableSection: Int, TableSection {
    case Username
    case Birthdate
    case Password
    case RepeatPassword

    static var rows: [RegisterTableSection] {
        return [.Username, .Password, .RepeatPassword]
    }

    var title: String {
        switch self {
        case .Username:
            return "Username"
        case .Birthdate:
            return "Date of birth"
        case .Password:
            return "Password"
        case .RepeatPassword:
            return "Repeat password"
        }
    }

    var mandatoryField: Bool {
        switch self {
        case .Username:
            return true
        case .Birthdate:
            return false
        case .Password:
            return true
        case .RepeatPassword:
            return true
        }
    }
}

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return RegisterTableSection.rows.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        guard let row = RegisterTableSection(rawValue: indexPath.row) else {
            // This should never happen
            return UITableViewCell()
        }

        let cell = UITableViewCell()
        cell.textLabel?.text = row.mandatoryTitle
        return cell

    }
}
Run Code Online (Sandbox Code Playgroud)

前面的代码将呈现下表:

枚举定义表

请注意,通过实现协议,我们的RegisterTableSection枚举必须为协议中定义的方法和变量提供实现.最有趣的是,它通过协议扩展继承了变量的默认实现mandatoryTitleTableSection

我已上载该示例的源代码在这里

  • 这是一个很好的答案,并显示了面向协议编程的另一个好处 (3认同)

Vas*_*huk 41

看看我的例子,它更容易:枚举是否可以在Swift中包含另一个枚举值?

理念

enum State {
    case started
    case succeeded
    case failed
}

enum ActionState {
    case state(value: State)
    case cancelled
}
Run Code Online (Sandbox Code Playgroud)

结果

ActionState实例现在有4个值:

.state(value: .started)
.state(value: .succeeded)
.state(value: .failed)
.cancelled
Run Code Online (Sandbox Code Playgroud)

另一个样本

import Foundation

enum StringCharactersTransformType {
    case upperCase
    case lowerCase
}

enum StringTransformType {
    case state(value: StringCharactersTransformType)
    case normal

    static var upperCase: StringTransformType {
        return .state(value: .upperCase)
    }

    static var lowerCase: StringTransformType {
        return .state(value: .lowerCase)
    }
}

var type = StringTransformType.normal
print(type)
type = .upperCase
print(type)
type = .lowerCase
print(type)
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述 在此输入图像描述