Skip to content

Commit 7de160d

Browse files
Merge pull request #980 from gcharita/master
Added support for Swift 4.2 and Xcode 10
2 parents 5884088 + 5985794 commit 7de160d

8 files changed

+143
-1
lines changed

Diff for: Sources/EnumOperators.swift

+9
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ public func >>> <T: RawRepresentable>(left: T?, right: Map) {
3131
}
3232

3333

34+
// Code targeting the Swift 4.1 compiler and below.
35+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
3436
/// Implicitly Unwrapped Optional Object of Raw Representable type
3537
public func <- <T: RawRepresentable>(left: inout T!, right: Map) {
3638
left <- (right, EnumTransform())
3739
}
40+
#endif
3841

3942
// MARK:- Arrays of Raw Representable type
4043

@@ -58,10 +61,13 @@ public func >>> <T: RawRepresentable>(left: [T]?, right: Map) {
5861
}
5962

6063

64+
// Code targeting the Swift 4.1 compiler and below.
65+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
6166
/// Array of Raw Representable object
6267
public func <- <T: RawRepresentable>(left: inout [T]!, right: Map) {
6368
left <- (right, EnumTransform())
6469
}
70+
#endif
6571

6672
// MARK:- Dictionaries of Raw Representable type
6773

@@ -85,7 +91,10 @@ public func >>> <T: RawRepresentable>(left: [String: T]?, right: Map) {
8591
}
8692

8793

94+
// Code targeting the Swift 4.1 compiler and below.
95+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
8896
/// Dictionary of Raw Representable object
8997
public func <- <T: RawRepresentable>(left: inout [String: T]!, right: Map) {
9098
left <- (right, EnumTransform())
9199
}
100+
#endif

Diff for: Sources/FromJSON.swift

+22-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ internal final class FromJSON {
4040
field = object
4141
}
4242

43+
// Code targeting the Swift 4.1 compiler and below.
44+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
4345
/// Implicitly unwrapped optional basic type
4446
class func optionalBasicType<FieldType>(_ field: inout FieldType!, object: FieldType?) {
4547
field = object
4648
}
49+
#endif
4750

4851
/// Mappable object
4952
class func object<N: BaseMappable>(_ field: inout N, map: Map) {
@@ -64,6 +67,8 @@ internal final class FromJSON {
6467
}
6568
}
6669

70+
// Code targeting the Swift 4.1 compiler and below.
71+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
6772
/// Implicitly unwrapped Optional Mappable Object
6873
class func optionalObject<N: BaseMappable>(_ field: inout N!, map: Map) {
6974
if let f = field , map.toObject && map.currentValue != nil {
@@ -72,6 +77,7 @@ internal final class FromJSON {
7277
field = Mapper(context: map.context).map(JSONObject: map.currentValue)
7378
}
7479
}
80+
#endif
7581

7682
/// mappable object array
7783
class func objectArray<N: BaseMappable>(_ field: inout Array<N>, map: Map) {
@@ -90,6 +96,8 @@ internal final class FromJSON {
9096
}
9197
}
9298

99+
// Code targeting the Swift 4.1 compiler and below.
100+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
93101
/// Implicitly unwrapped optional mappable object array
94102
class func optionalObjectArray<N: BaseMappable>(_ field: inout Array<N>!, map: Map) {
95103
if let objects: Array<N> = Mapper(context: map.context).mapArray(JSONObject: map.currentValue) {
@@ -98,6 +106,7 @@ internal final class FromJSON {
98106
field = nil
99107
}
100108
}
109+
#endif
101110

102111
/// mappable object array
103112
class func twoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>, map: Map) {
@@ -111,10 +120,13 @@ internal final class FromJSON {
111120
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
112121
}
113122

123+
// Code targeting the Swift 4.1 compiler and below.
124+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
114125
/// Implicitly unwrapped optional 2 dimentional mappable object array
115126
class func optionalTwoDimensionalObjectArray<N: BaseMappable>(_ field: inout Array<Array<N>>!, map: Map) {
116127
field = Mapper(context: map.context).mapArrayOfArrays(JSONObject: map.currentValue)
117128
}
129+
#endif
118130

119131
/// Dctionary containing Mappable objects
120132
class func objectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>, map: Map) {
@@ -136,6 +148,8 @@ internal final class FromJSON {
136148
}
137149
}
138150

151+
// Code targeting the Swift 4.1 compiler and below.
152+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
139153
/// Implicitly unwrapped Dictionary containing Mappable objects
140154
class func optionalObjectDictionary<N: BaseMappable>(_ field: inout Dictionary<String, N>!, map: Map) {
141155
if let f = field , map.toObject && map.currentValue != nil {
@@ -144,6 +158,7 @@ internal final class FromJSON {
144158
field = Mapper(context: map.context).mapDictionary(JSONObject: map.currentValue)
145159
}
146160
}
161+
#endif
147162

148163
/// Dictionary containing Array of Mappable objects
149164
class func objectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>, map: Map) {
@@ -157,10 +172,13 @@ internal final class FromJSON {
157172
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
158173
}
159174

175+
// Code targeting the Swift 4.1 compiler and below.
176+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
160177
/// Implicitly unwrapped Dictionary containing Array of Mappable objects
161178
class func optionalObjectDictionaryOfArrays<N: BaseMappable>(_ field: inout Dictionary<String, [N]>!, map: Map) {
162179
field = Mapper<N>(context: map.context).mapDictionaryOfArrays(JSONObject: map.currentValue)
163180
}
181+
#endif
164182

165183
/// mappable object Set
166184
class func objectSet<N: BaseMappable>(_ field: inout Set<N>, map: Map) {
@@ -174,8 +192,11 @@ internal final class FromJSON {
174192
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
175193
}
176194

195+
// Code targeting the Swift 4.1 compiler and below.
196+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
177197
/// Implicitly unwrapped optional mappable object array
178198
class func optionalObjectSet<N: BaseMappable>(_ field: inout Set<N>!, map: Map) {
179199
field = Mapper(context: map.context).mapSet(JSONObject: map.currentValue)
180-
}
200+
}
201+
#endif
181202
}

Diff for: Sources/ImmutableMappable.swift

+4
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ public extension Mapper where N: ImmutableMappable {
214214
// MARK: Array mapping functions
215215

216216
public func mapArray(JSONArray: [[String: Any]]) throws -> [N] {
217+
#if swift(>=4.1)
217218
return try JSONArray.compactMap(mapOrFail)
219+
#else
220+
return try JSONArray.flatMap(mapOrFail)
221+
#endif
218222
}
219223

220224
public func mapArray(JSONString: String) throws -> [N] {

Diff for: Sources/IntegerOperators.swift

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public func <- <T: SignedInteger>(left: inout T?, right: Map) {
3434
}
3535
}
3636

37+
// Code targeting the Swift 4.1 compiler and below.
38+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
3739
/// ImplicitlyUnwrappedOptional SignedInteger mapping
3840
public func <- <T: SignedInteger>(left: inout T!, right: Map) {
3941
switch right.mappingType {
@@ -45,6 +47,7 @@ public func <- <T: SignedInteger>(left: inout T!, right: Map) {
4547
default: ()
4648
}
4749
}
50+
#endif
4851

4952

5053
// MARK: - Unsigned Integer
@@ -74,6 +77,8 @@ public func <- <T: UnsignedInteger>(left: inout T?, right: Map) {
7477
}
7578
}
7679

80+
// Code targeting the Swift 4.1 compiler and below.
81+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
7782
/// ImplicitlyUnwrappedOptional UnsignedInteger mapping
7883
public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
7984
switch right.mappingType {
@@ -85,6 +90,7 @@ public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
8590
default: ()
8691
}
8792
}
93+
#endif
8894

8995
// MARK: - Casting Utils
9096

Diff for: Sources/Map.swift

+4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ public final class Map {
131131
}
132132
} else if value == nil && T.self == [Float].self {
133133
if let v = currentValue as? [Double] {
134+
#if swift(>=4.1)
134135
return v.compactMap{ Float($0) } as? T
136+
#else
137+
return v.flatMap{ Float($0) } as? T
138+
#endif
135139
}
136140
} else if value == nil && T.self == [String:Float].self {
137141
if let v = currentValue as? [String:Double] {

Diff for: Sources/Mapper.swift

+8
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ public final class Mapper<N: BaseMappable> {
159159
/// Maps an array of JSON dictionary to an array of Mappable objects
160160
public func mapArray(JSONArray: [[String: Any]]) -> [N] {
161161
// map every element in JSON array to type N
162+
#if swift(>=4.1)
163+
let result = JSONArray.compactMap(map)
164+
#else
162165
let result = JSONArray.flatMap(map)
166+
#endif
163167
return result
164168
}
165169

@@ -425,7 +429,11 @@ extension Mapper where N: Hashable {
425429
/// Maps an Set of JSON dictionary to an array of Mappable objects
426430
public func mapSet(JSONArray: [[String: Any]]) -> Set<N> {
427431
// map every element in JSON array to type N
432+
#if swift(>=4.1)
428433
return Set(JSONArray.compactMap(map))
434+
#else
435+
return Set(JSONArray.flatMap(map))
436+
#endif
429437
}
430438

431439
///Maps a Set of Objects to a Set of JSON dictionaries [[String : Any]]

Diff for: Sources/Operators.swift

+21
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public func >>> <T>(left: T?, right: Map) {
7676
}
7777

7878

79+
// Code targeting the Swift 4.1 compiler and below.
80+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
7981
/// Implicitly unwrapped optional object of basic type
8082
public func <- <T>(left: inout T!, right: Map) {
8183
switch right.mappingType {
@@ -86,6 +88,7 @@ public func <- <T>(left: inout T!, right: Map) {
8688
default: ()
8789
}
8890
}
91+
#endif
8992

9093
// MARK:- Mappable Objects - <T: BaseMappable>
9194

@@ -124,6 +127,8 @@ public func >>> <T: BaseMappable>(left: T?, right: Map) {
124127
}
125128

126129

130+
// Code targeting the Swift 4.1 compiler and below.
131+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
127132
/// Implicitly unwrapped optional Mappable objects
128133
public func <- <T: BaseMappable>(left: inout T!, right: Map) {
129134
switch right.mappingType {
@@ -134,6 +139,7 @@ public func <- <T: BaseMappable>(left: inout T!, right: Map) {
134139
default: ()
135140
}
136141
}
142+
#endif
137143

138144
// MARK:- Dictionary of Mappable objects - Dictionary<String, T: BaseMappable>
139145

@@ -173,6 +179,8 @@ public func >>> <T: BaseMappable>(left: Dictionary<String, T>?, right: Map) {
173179
}
174180

175181

182+
// Code targeting the Swift 4.1 compiler and below.
183+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
176184
/// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
177185
public func <- <T: BaseMappable>(left: inout Dictionary<String, T>!, right: Map) {
178186
switch right.mappingType {
@@ -183,6 +191,7 @@ public func <- <T: BaseMappable>(left: inout Dictionary<String, T>!, right: Map)
183191
default: ()
184192
}
185193
}
194+
#endif
186195

187196
/// Dictionary of Mappable objects <String, T: Mappable>
188197
public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>, right: Map) {
@@ -219,6 +228,8 @@ public func >>> <T: BaseMappable>(left: Dictionary<String, [T]>?, right: Map) {
219228
}
220229

221230

231+
// Code targeting the Swift 4.1 compiler and below.
232+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
222233
/// Implicitly unwrapped Optional Dictionary of Mappable object <String, T: Mappable>
223234
public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>!, right: Map) {
224235
switch right.mappingType {
@@ -229,6 +240,7 @@ public func <- <T: BaseMappable>(left: inout Dictionary<String, [T]>!, right: Ma
229240
default: ()
230241
}
231242
}
243+
#endif
232244

233245
// MARK:- Array of Mappable objects - Array<T: BaseMappable>
234246

@@ -267,6 +279,8 @@ public func >>> <T: BaseMappable>(left: Array<T>?, right: Map) {
267279
}
268280

269281

282+
// Code targeting the Swift 4.1 compiler and below.
283+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
270284
/// Implicitly unwrapped Optional array of Mappable objects
271285
public func <- <T: BaseMappable>(left: inout Array<T>!, right: Map) {
272286
switch right.mappingType {
@@ -277,6 +291,7 @@ public func <- <T: BaseMappable>(left: inout Array<T>!, right: Map) {
277291
default: ()
278292
}
279293
}
294+
#endif
280295

281296
// MARK:- Array of Array of Mappable objects - Array<Array<T: BaseMappable>>
282297

@@ -316,6 +331,8 @@ public func >>> <T: BaseMappable>(left: Array<Array<T>>?, right: Map) {
316331
}
317332

318333

334+
// Code targeting the Swift 4.1 compiler and below.
335+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
319336
/// Implicitly unwrapped Optional array of Mappable objects
320337
public func <- <T: BaseMappable>(left: inout Array<Array<T>>!, right: Map) {
321338
switch right.mappingType {
@@ -326,6 +343,7 @@ public func <- <T: BaseMappable>(left: inout Array<Array<T>>!, right: Map) {
326343
default: ()
327344
}
328345
}
346+
#endif
329347

330348
// MARK:- Set of Mappable objects - Set<T: BaseMappable>
331349

@@ -365,6 +383,8 @@ public func >>> <T: BaseMappable>(left: Set<T>?, right: Map) {
365383
}
366384

367385

386+
// Code targeting the Swift 4.1 compiler and below.
387+
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
368388
/// Implicitly unwrapped Optional Set of Mappable objects
369389
public func <- <T: BaseMappable>(left: inout Set<T>!, right: Map) {
370390
switch right.mappingType {
@@ -375,3 +395,4 @@ public func <- <T: BaseMappable>(left: inout Set<T>!, right: Map) {
375395
default: ()
376396
}
377397
}
398+
#endif

0 commit comments

Comments
 (0)