Swift enum inheritance Swift enum inheritance ios ios

Swift enum inheritance


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.

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

stackOverflow difference classes vs structs


As Korpel answered already, currently there is no actual inheritance supported for Enums. So it is not possible to have a certain Enum extend and inherit the cases of another enum.

However, I would add for completion, that Enums do support protocols, and together with protocol extensions introduced in Swift 2 and the new protocol-oriented programming approach (see this video), it is possible to implement something that resembles inheritance. This is a technique I use a lot to define UITableViewController:s driven by enums, to specify the sections of the table, and the rows within each section, and to add some useful behaviour. See for example the following sample code:

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

The previous code would render the following table:

Enum-defined table

Notice how by implementing the protocol, our RegisterTableSection enum has to provide implementations to methods and variables defined in the protocol. And most interestingly, it inherits a default implementation of the variable mandatoryTitle through the TableSection protocol extension

I have uploaded the source code of this example here


Look at my example, it is much easier: Can an enum contain another enum values in Swift?

Details

Tested on:

  • Xcode 9.2, Swift 4 and 3
  • Xcode 10.2 (10E125) and 11.0 (11A420a), Swift 5

Solution

enum State {    case started    case succeeded    case failed}enum ActionState {    case state(value: State)    case cancelled}

Result

ActionState enum has 4 values:

.state(value: .started).state(value: .succeeded).state(value: .failed).cancelled

Another sample

import Foundationenum 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.normalprint(type)type = .upperCaseprint(type)type = .lowerCaseprint(type)

Result

enter image description hereenter image description here