-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEnum.swift
More file actions
executable file
·131 lines (116 loc) · 3.46 KB
/
Enum.swift
File metadata and controls
executable file
·131 lines (116 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// EnumExtensions.swift
// Stuff
//
// Created by Edwin Vermeer on 10/02/2017.
// Copyright © 2017 EVICT BV. All rights reserved.
//
/**
Protocol for the enum extension functions
*/
public protocol Enum {
}
/**
Protocol for the workaround when using an enum with a rawValue of an undefined type
*/
public protocol RawEnum {
/**
For implementing a function that will return the rawValue for a non sepecific enum
*/
var anyRawValue: Any { get }
}
/**
Enum extension for getting the associated value of an enum
*/
public extension Enum {
/**
Get the associated value of the enum
*/
var associated: (label: String, value: Any?, values: [Any]) {
get {
let mirror = Mirror(reflecting: self)
if mirror.displayStyle == .enum {
if let associated = mirror.children.first {
let values = Mirror(reflecting: associated.value).children
var valuesArray = [Any]()
for item in values {
valuesArray.append(item.value)
}
return (associated.label!, associated.value, valuesArray)
}
print("WARNING: Enum option of \(self) does not have an associated value")
return ("\(self)", nil, [])
}
print("WARNING: You can only extend an enum with the EnumExtension")
return ("\(self)", nil, [])
}
}
}
/**
Enum extension for getting all enum options in an array
*/
public extension Enum where Self: Hashable {
/**
Return all enum values
*/
static var allValues: [Self] {
return [Self](AnySequence { () -> AnyIterator<Self> in
var raw = 0
var first: Self?
return AnyIterator {
let current = withUnsafeBytes(of: &raw) { $0.load(as: Self.self) }
if raw == 0 {
first = current
} else if current == first {
return nil
}
raw += 1
return current
}
})
}
}
/**
Enum extension for getting the rawValue even if the enum is passed in an Enum
*/
public extension RawEnum where Self: RawRepresentable {
/**
Return the raw value of the enum
*/
var anyRawValue: Any {
get {
let mirror = Mirror(reflecting: self)
if mirror.displayStyle != .enum {
print("WARNING: You can only extend an enum with the Enum protocol")
}
return rawValue as Any
}
}
}
/**
extension for creating a querystring from an array of Enum values (with associated value)
*/
public extension Array where Element: Enum {
var queryString: String {
get {
return (self.map {"\($0.associated.label)=\($0.associated.value!)"}).joined(separator: ",")
}
}
}
/**
Dictionary extension for creating a dictionary from an array of enum values
*/
public extension Dictionary {
/**
Create a dictionairy based on all associated values of an enum array
- parameter associated: array of dictionairy values which have an associated value
*/
init<T: Enum>(_ associated: [T]?) {
self.init()
if associated != nil {
for myEnum in associated! {
self[(myEnum.associated.label as? Key)!] = myEnum.associated.value as? Value
}
}
}
}