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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// ArrayExtension.swift
// EVReflection
//
// Created by Edwin Vermeer on 9/2/15.
// Copyright © 2015 evict. All rights reserved.
//
import Foundation
/**
Extending Array with an some EVReflection functions where the elements can be of type NSObject
*/
public extension Array where Element: NSObject {
/**
Initialize an array based on a json string
- parameter json: The json string
- parameter conversionOptions: Option set for the various conversion options.
*/
init(json: String?, conversionOptions: ConversionOptions = .DefaultDeserialize, forKeyPath: String? = nil) {
self.init()
let arrayTypeInstance = getArrayTypeInstance(self)
let newArray = EVReflection.arrayFromJson(type: arrayTypeInstance, json: json, conversionOptions: conversionOptions, forKeyPath: forKeyPath)
for item in newArray {
self.append(item)
}
}
/**
Initialize an array based on a json string
- parameter json: The json string
- parameter conversionOptions: Option set for the various conversion options.
*/
init(data: Data?, conversionOptions: ConversionOptions = .DefaultDeserialize, forKeyPath: String? = nil) {
self.init()
let arrayTypeInstance = getArrayTypeInstance(self)
let newArray = EVReflection.arrayFromData(nil, type:arrayTypeInstance, data: data, conversionOptions: conversionOptions, forKeyPath: forKeyPath)
for item in newArray {
self.append(item)
}
}
/**
Initialize an array based on a dictionary
- parameter json: The json string
- parameter conversionOptions: Option set for the various conversion options.
*/
init(dictionaryArray: [NSDictionary], conversionOptions: ConversionOptions = .DefaultDeserialize) {
self.init()
for item in dictionaryArray {
let arrayTypeInstance = getArrayTypeInstance(self)
EVReflection.setPropertiesfromDictionary(item, anyObject: arrayTypeInstance)
self.append(arrayTypeInstance)
}
}
/**
Initialize an array based on a dictionary
- parameter json: The json string
- parameter conversionOptions: Option set for the various conversion options.
*/
init(dictionary: NSDictionary, forKeyPath: String, conversionOptions: ConversionOptions = .DefaultDeserialize) {
self.init()
guard let dictionaryArray = dictionary.value(forKeyPath: forKeyPath) as? [NSDictionary] else {
evPrint(.UnknownKeypath, "ERROR: The forKeyPath '\(forKeyPath)' resulted in an empty array")
return
}
for item in dictionaryArray {
let arrayTypeInstance = getArrayTypeInstance(self)
EVReflection.setPropertiesfromDictionary(item, anyObject: arrayTypeInstance)
self.append(arrayTypeInstance)
}
}
/**
Get the type of the object where this array is for
- parameter arr: this array
- returns: The object type
*/
func getArrayTypeInstance<T: NSObject>(_ arr: Array<T>) -> T {
return arr.getTypeInstance()
}
/**
Get the type of the object where this array is for
- returns: The object type
*/
func getTypeInstance<T: NSObject>(
) -> T {
let nsobjectype: NSObject.Type = T.self
let nsobject: NSObject = nsobjectype.init()
if let obj = nsobject as? T {
return obj
}
// Could not instantiate array item instance.
return T()
}
/**
Get the string representation of the type of the object where this array is for
- returns: The object type
*/
func getTypeAsString() -> String {
let item = self.getTypeInstance()
return NSStringFromClass(type(of:item))
}
}
/**
Extending Array with an some EVReflection functions where the elements can be of type EVReflectable
*/
public extension Array where Element: EVReflectable {
/**
Convert this array to a json string
- parameter conversionOptions: Option set for the various conversion options.
- parameter prettyPrinted: Define if you want enters and indents
- returns: The json string
*/
func toJsonString(_ conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> String {
return "[\n" + self.map({($0).toJsonString(conversionOptions, prettyPrinted: prettyPrinted)}).joined(separator: ", \n") + "\n]"
}
/**
Convert this array to a json data
- parameter conversionOptions: Option set for the various conversion options.
- parameter prettyPrinted: Define if you want enters and indents
- parameter encoding: The string encoding defaulsts to .utf8
- returns: The json data
*/
func toJsonData(_ conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false, encoding: String.Encoding = .utf8) -> Data {
return self.toJsonString(conversionOptions, prettyPrinted: prettyPrinted).data(using: encoding) ?? Data()
}
/**
Returns the dictionary representation of this array.
- parameter conversionOptions: Option set for the various conversion options.
- returns: The array of dictionaries
*/
func toDictionaryArray(_ conversionOptions: ConversionOptions = .DefaultSerialize) -> NSArray {
return self.map({($0).toDictionary(conversionOptions)}) as NSArray
}
}
/**
Extending Array with an some EVReflection functions where the elements can be of type NSObject
*/
public extension Array where Element: NSDictionary {
/**
Initialize a dictionary array based on a json string
- parameter json: The json string
*/
init(jsonArray: String) {
self.init()
let dictArray = EVReflection.dictionaryArrayFromJson(jsonArray)
for item in dictArray {
self.append(item as! Element)
}
}
/**
Initialize a dictionary array based on a json string
- parameter json: The json string
*/
init(dataArray: Data) {
self.init(jsonArray: String(data: dataArray, encoding: .utf8) ?? "")
}
/**
Convert this array to a json string
- parameter conversionOptions: Option set for the various conversion options.
- returns: The json string
*/
func toJsonStringArray(prettyPrinted: Bool = false) -> String {
let jsonArray: [String] = self.map { ($0 as NSDictionary).toJsonString(prettyPrinted: prettyPrinted) as String }
return "[\n" + jsonArray.joined(separator: ", \n") + "\n]"
}
}
public extension NSArray {
func nestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[T]] {
return (self.map {
(($0 as? NSArray)?.map {
element($0 as? NSDictionary ?? NSDictionary())
}) ?? []
})
}
func doubleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[T]]] {
return (self.map {
(($0 as? NSArray)?.nestedArrayMap { element($0) }) ?? [[]]
})
}
func tripleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[T]]]] {
return (self.map {
(($0 as? NSArray)?.doubleNestedArrayMap { element($0) }) ?? [[[]]]
})
}
func quadrupleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[[T]]]]] {
return (self.map {
(($0 as? NSArray)?.tripleNestedArrayMap { element($0) }) ?? [[[[]]]]
})
}
func quintupleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[[[T]]]]]] {
return (self.map {
(($0 as? NSArray)?.quadrupleNestedArrayMap { element($0) }) ?? [[[[[]]]]]
})
}
func sextupleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[[[[T]]]]]]] {
return (self.map {
(($0 as? NSArray)?.quintupleNestedArrayMap { element($0) }) ?? [[[[[[]]]]]]
})
}
// If you need deeper nesting, whell, then you probably see the pattern above that you need to implement :-)
// just name them septuple, octuple, nonuple and decuple
// I'm not sure how far swift can handle it, but you should not want something like that.
}