Skip to content

Instantly share code, notes, and snippets.

View polac24's full-sized avatar

Bartosz Polaczyk polac24

  • Apple
  • Cupertino
View GitHub Profile
@polac24
polac24 / IntEnum.swift
Last active August 30, 2017 10:46
Enum with Int
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) {
enum BestEnum: String{
case myCase = "my_case"
case otherCase = "other_case"
}
BestENum.allCases // [.myCase, .otherCase]
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
}
struct BestEnumRaw: ExpressibleByStringLiteral{
let rawString:String?
init(){
rawString = nil
}
init(stringLiteral value: String){
rawString = value
}
extension BestEnumRaw: Equatable{
static func ==(lhs: BestEnumRaw, rhs: BestEnumRaw) -> Bool {
return lhs.rawString == rhs.rawString
}
}
enum BestEnum: BestEnumRaw{
case myCase = "my_case"
case otherCase = "other_case"
}
enum BestEnum:BestEnumRaw{
static var all:Set<String> = []
case myCase = "my_case"
case otherCase = "other_case"
}
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
}
}
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())
import EnumList
enum BestEnum: EnumListStringRaw<BestEnum.Values>, RawRepresentable{
struct Values:StringEnumValues {
typealias Element = BestEnum
static var allRaws:Set<String> = []
}
case myCase = "my_case"
case otherCase = "otherCase"