Last active
March 5, 2018 12:16
-
-
Save laevandus/b2f4999a1c9c792e61efb12d5b38ddce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Scene { | |
case home | |
case level(Int) | |
case levelSelection | |
} | |
extension Scene: RawRepresentable { | |
typealias RawValue = String | |
init?(rawValue: String) { | |
switch rawValue { | |
case "home": | |
self = .home | |
case "levelSelection": | |
self = .levelSelection | |
default: | |
guard let range = rawValue.range(of: "level", options: .anchored) else { return nil } | |
guard let number = Int(rawValue.suffix(from: range.upperBound)) else { return nil } | |
self = .level(number) | |
} | |
} | |
var rawValue: String { | |
switch self { | |
case .home: | |
return "home" | |
case .levelSelection: | |
return "levelSelection" | |
case .level(let number): | |
return "level\(number)" | |
} | |
} | |
} | |
// Examples: | |
// Scene.home | |
let home = Scene(rawValue: "home") | |
// Scene.level(1) | |
let level1 = Scene(rawValue: "level1") | |
let paddedLevel1 = Scene(rawValue: "level001") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment