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 Cases: Int { | |
case myCase = 1 | |
case myOtherCase | |
// custom value breaks this workaround | |
// case usafeCase = 300 | |
static var allCases: [Cases] { | |
var values: [Cases] = [] | |
var index = 1 | |
while let element = self.init(rawValue: index) { |
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 BestEnum: String{ | |
case myCase = "my_case" | |
case otherCase = "other_case" | |
} | |
BestENum.allCases // [.myCase, .otherCase] |
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 BestEnum:RawRepresentable{ | |
case myCase | |
case otherCase | |
init?(rawValue: String) { | |
switch (rawValue){ | |
case "my_case": self = .myCase | |
case "other_case": self = .otherCase | |
default: return nil | |
} |
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
struct BestEnumRaw: ExpressibleByStringLiteral{ | |
let rawString:String? | |
init(){ | |
rawString = nil | |
} | |
init(stringLiteral value: String){ | |
rawString = value | |
} |
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
extension BestEnumRaw: Equatable{ | |
static func ==(lhs: BestEnumRaw, rhs: BestEnumRaw) -> Bool { | |
return lhs.rawString == rhs.rawString | |
} | |
} |
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 BestEnum: BestEnumRaw{ | |
case myCase = "my_case" | |
case otherCase = "other_case" | |
} |
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 BestEnum:BestEnumRaw{ | |
static var all:Set<String> = [] | |
case myCase = "my_case" | |
case otherCase = "other_case" | |
} |
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
extension BestEnumRaw: Equatable{ | |
static func ==(lhs: BestEnumRaw, rhs: BestEnumRaw) -> Bool { | |
if let lhsRaw = lhs.rawString { BestEnum.all.insert(lhsRaw)} | |
if let rhsRaw = rhs.rawString { BestEnum.all.insert(rhsRaw)} | |
return lhs.rawString == rhs.rawString | |
} | |
} |
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 BestEnum:BestEnumRaw{ | |
static var all:Set<String> = [] | |
case myCase = "my_case" | |
case otherCase = "otherCase" | |
} | |
// ensuring that all static var fills with raw values | |
BestEnum(rawValue: BestEnumRaw()) |
OlderNewer