Skip to content

Commit 556a9cf

Browse files
committed
Make Mapper and Map more immutable
1 parent 724e957 commit 556a9cf

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

ObjectMapper/Core/Mapper.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ enum MappingType {
2222
* A class used for holding mapping data
2323
*/
2424
public 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
/**
@@ -74,8 +76,6 @@ public class Map {
7476
}
7577

7678
public class Mapper<N: Mappable> {
77-
var map = Map()
78-
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)