Skip to content

Commit 54ced9d

Browse files
Merge pull request tristanhimmelman#49 from ikesyo/immutability
Make Mapper and Map more immutable
2 parents 724e957 + 46d406b commit 54ced9d

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

ObjectMapper/Core/Mapper.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ enum MappingType {
2121
/**
2222
* A class used for holding mapping data
2323
*/
24-
public class Map {
24+
public final class Map {
25+
let mappingType: MappingType
26+
2527
var JSONDictionary: [String : AnyObject] = [:]
2628
var currentValue: AnyObject?
2729
var currentKey: String?
28-
var mappingType: MappingType = .fromJSON
2930

30-
public init(){
31-
31+
private init(mappingType: MappingType, JSONDictionary: [String : AnyObject]) {
32+
self.mappingType = mappingType
33+
self.JSONDictionary = JSONDictionary
3234
}
3335

3436
/**
@@ -73,9 +75,7 @@ public class Map {
7375
}
7476
}
7577

76-
public class Mapper<N: Mappable> {
77-
var map = Map()
78-
78+
public final class Mapper<N: Mappable> {
7979
public init(){
8080

8181
}
@@ -115,8 +115,7 @@ public class Mapper<N: Mappable> {
115115
* Usefull for those pesky objects that have crappy designated initializers like NSManagedObject
116116
*/
117117
public func map(JSON: [String : AnyObject], var toObject object: N) -> N! {
118-
map.mappingType = .fromJSON
119-
map.JSONDictionary = JSON
118+
let map = Map(mappingType: .fromJSON, JSONDictionary: JSON)
120119
object.map(map)
121120
return object
122121
}
@@ -153,11 +152,8 @@ public class Mapper<N: Mappable> {
153152
* Maps an object that conforms to Mappable to a JSON dictionary <String : AnyObject>
154153
*/
155154
public func toJSON(var object: N) -> [String : AnyObject] {
156-
map.mappingType = .toJSON
157-
map.JSONDictionary = [String : AnyObject]()
158-
155+
let map = Map(mappingType: .toJSON, JSONDictionary: [:])
159156
object.map(map)
160-
161157
return map.JSONDictionary
162158
}
163159

0 commit comments

Comments
 (0)