Swift enum inheritance

IosSwiftInheritanceEnumsRawrepresentable

Ios Problem Overview


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

The following test code:

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

enum TemperatureSubunit : Temperature {  
}

generates

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

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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][1]), 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 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
		
	}
}

The previous code would render the following table:

[![Enum-defined table][2]][3]

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][4]

[1]: https://developer.apple.com/videos/play/wwdc2015/408/ "this video" [2]: http://i.stack.imgur.com/rMW5Am.png [3]: http://i.stack.imgur.com/rMW5A.png [4]: https://github.com/lrmunoz/SwiftCodeSamples/tree/master/EnumDrivenTableTest "here"

Solution 3 - Ios

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

Result

enter image description here enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoonView Question on Stackoverflow
Solution 1 - IosKorpelView Answer on Stackoverflow
Solution 2 - IosLuisView Answer on Stackoverflow
Solution 3 - IosVasily BodnarchukView Answer on Stackoverflow