Skip to content

Commit bedf768

Browse files
Merge pull request tristanhimmelman#99 from ikesyo/no-instantiation
Change functions of `FromJSON` and `ToJSON` to `class func`, make the classes final
2 parents 74fd447 + f4c2c7b commit bedf768

File tree

3 files changed

+56
-60
lines changed

3 files changed

+56
-60
lines changed

ObjectMapper/Core/FromJSON.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,60 @@
66
// Copyright (c) 2014 hearst. All rights reserved.
77
//
88

9-
import Foundation
10-
11-
class FromJSON {
9+
internal final class FromJSON {
1210

1311
/// Basic type
14-
func basicType<FieldType>(inout field: FieldType, object: AnyObject?) {
12+
class func basicType<FieldType>(inout field: FieldType, object: AnyObject?) {
1513
basicType(&field, object: object as? FieldType)
1614
}
1715

18-
func basicType<FieldType>(inout field: FieldType, object: FieldType?) {
16+
class func basicType<FieldType>(inout field: FieldType, object: FieldType?) {
1917
if let value = object {
2018
field = value
2119
}
2220
}
2321

2422
/// optional basic type
25-
func optionalBasicType<FieldType>(inout field: FieldType?, object: AnyObject?) {
23+
class func optionalBasicType<FieldType>(inout field: FieldType?, object: AnyObject?) {
2624
optionalBasicType(&field, object: object as? FieldType)
2725
}
2826

29-
func optionalBasicType<FieldType>(inout field: FieldType?, object: FieldType?) {
27+
class func optionalBasicType<FieldType>(inout field: FieldType?, object: FieldType?) {
3028
if let value: FieldType = object {
3129
field = value
3230
}
3331
}
3432

3533
/// Implicitly unwrapped optional basic type
36-
func optionalBasicType<FieldType>(inout field: FieldType!, object: AnyObject?) {
34+
class func optionalBasicType<FieldType>(inout field: FieldType!, object: AnyObject?) {
3735
optionalBasicType(&field, object: object as? FieldType)
3836
}
3937

40-
func optionalBasicType<FieldType>(inout field: FieldType!, object: FieldType?) {
38+
class func optionalBasicType<FieldType>(inout field: FieldType!, object: FieldType?) {
4139
if let value: FieldType = object {
4240
field = value
4341
}
4442
}
4543

4644
/// Mappable object
47-
func object<N: Mappable>(inout field: N, object: AnyObject?) {
45+
class func object<N: Mappable>(inout field: N, object: AnyObject?) {
4846
if let value: N = Mapper().map(object) {
4947
field = value
5048
}
5149
}
5250

5351
/// Optional Mappable Object
54-
func optionalObject<N: Mappable>(inout field: N?, object: AnyObject?) {
52+
class func optionalObject<N: Mappable>(inout field: N?, object: AnyObject?) {
5553
field = Mapper().map(object)
5654
}
5755

5856
/// Implicitly unwrapped Optional Mappable Object
59-
func optionalObject<N: Mappable>(inout field: N!, object: AnyObject?) {
57+
class func optionalObject<N: Mappable>(inout field: N!, object: AnyObject?) {
6058
field = Mapper().map(object)
6159
}
6260

6361
/// mappable object array
64-
func objectArray<N: Mappable>(inout field: Array<N>, object: AnyObject?) {
62+
class func objectArray<N: Mappable>(inout field: Array<N>, object: AnyObject?) {
6563
let parsedObjects = Mapper<N>().mapArray(object)
6664

6765
if let objects = parsedObjects {
@@ -70,17 +68,17 @@ class FromJSON {
7068
}
7169

7270
/// optional mappable object array
73-
func optionalObjectArray<N: Mappable>(inout field: Array<N>?, object: AnyObject?) {
71+
class func optionalObjectArray<N: Mappable>(inout field: Array<N>?, object: AnyObject?) {
7472
field = Mapper().mapArray(object)
7573
}
7674

7775
/// Implicitly unwrapped optional mappable object array
78-
func optionalObjectArray<N: Mappable>(inout field: Array<N>!, object: AnyObject?) {
76+
class func optionalObjectArray<N: Mappable>(inout field: Array<N>!, object: AnyObject?) {
7977
field = Mapper().mapArray(object)
8078
}
8179

8280
/// Dctionary containing Mappable objects
83-
func objectDictionary<N: Mappable>(inout field: Dictionary<String, N>, object: AnyObject?) {
81+
class func objectDictionary<N: Mappable>(inout field: Dictionary<String, N>, object: AnyObject?) {
8482
let parsedObjects = Mapper<N>().mapDictionary(object)
8583

8684
if let objects = parsedObjects {
@@ -89,12 +87,12 @@ class FromJSON {
8987
}
9088

9189
/// Optional dictionary containing Mappable objects
92-
func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>?, object: AnyObject?) {
90+
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>?, object: AnyObject?) {
9391
field = Mapper().mapDictionary(object)
9492
}
9593

9694
/// Implicitly unwrapped Dictionary containing Mappable objects
97-
func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>!, object: AnyObject?) {
95+
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>!, object: AnyObject?) {
9896
field = Mapper().mapDictionary(object)
9997
}
10098
}

ObjectMapper/Core/Operators.swift

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// Copyright (c) 2014 hearst. All rights reserved.
77
//
88

9-
import Foundation
10-
119
/**
1210
* This file defines a new operator which is used to create a mapping between an object and a JSON key value.
1311
* There is an overloaded operator definition for each type of object that is supported in ObjectMapper.
@@ -23,9 +21,9 @@ infix operator <- {}
2321

2422
public func <- <T>(inout left: T, right: Map) {
2523
if right.mappingType == MappingType.fromJSON {
26-
FromJSON().basicType(&left, object: right.currentValue)
24+
FromJSON.basicType(&left, object: right.currentValue)
2725
} else {
28-
ToJSON().basicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
26+
ToJSON.basicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
2927
}
3028
}
3129

@@ -34,9 +32,9 @@ public func <- <T>(inout left: T, right: Map) {
3432
*/
3533
public func <- <T>(inout left: T?, right: Map) {
3634
if right.mappingType == MappingType.fromJSON {
37-
FromJSON().optionalBasicType(&left, object: right.currentValue)
35+
FromJSON.optionalBasicType(&left, object: right.currentValue)
3836
} else {
39-
ToJSON().optionalBasicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
37+
ToJSON.optionalBasicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
4038
}
4139
}
4240

@@ -45,9 +43,9 @@ public func <- <T>(inout left: T?, right: Map) {
4543
*/
4644
public func <- <T>(inout left: T!, right: Map) {
4745
if right.mappingType == MappingType.fromJSON {
48-
FromJSON().optionalBasicType(&left, object: right.currentValue)
46+
FromJSON.optionalBasicType(&left, object: right.currentValue)
4947
} else {
50-
ToJSON().optionalBasicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
48+
ToJSON.optionalBasicType(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
5149
}
5250
}
5351

@@ -57,10 +55,10 @@ public func <- <T>(inout left: T!, right: Map) {
5755
public func <- <T, Transform: TransformType where Transform.Object == T>(inout left: T, right: (Map, Transform)) {
5856
if right.0.mappingType == MappingType.fromJSON {
5957
var value: T? = right.1.transformFromJSON(right.0.currentValue)
60-
FromJSON().basicType(&left, object: value)
58+
FromJSON.basicType(&left, object: value)
6159
} else {
6260
var value: Transform.JSON? = right.1.transformToJSON(left)
63-
ToJSON().optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
61+
ToJSON.optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
6462
}
6563
}
6664

@@ -70,10 +68,10 @@ public func <- <T, Transform: TransformType where Transform.Object == T>(inout l
7068
public func <- <T, Transform: TransformType where Transform.Object == T>(inout left: T?, right: (Map, Transform)) {
7169
if right.0.mappingType == MappingType.fromJSON {
7270
var value: T? = right.1.transformFromJSON(right.0.currentValue)
73-
FromJSON().optionalBasicType(&left, object: value)
71+
FromJSON.optionalBasicType(&left, object: value)
7472
} else {
7573
var value: Transform.JSON? = right.1.transformToJSON(left)
76-
ToJSON().optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
74+
ToJSON.optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
7775
}
7876
}
7977

@@ -83,10 +81,10 @@ public func <- <T, Transform: TransformType where Transform.Object == T>(inout l
8381
public func <- <T, Transform: TransformType where Transform.Object == T>(inout left: T!, right: (Map, Transform)) {
8482
if right.0.mappingType == MappingType.fromJSON {
8583
var value: T? = right.1.transformFromJSON(right.0.currentValue)
86-
FromJSON().optionalBasicType(&left, object: value)
84+
FromJSON.optionalBasicType(&left, object: value)
8785
} else {
8886
var value: Transform.JSON? = right.1.transformToJSON(left)
89-
ToJSON().optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
87+
ToJSON.optionalBasicType(value, key: right.0.currentKey!, dictionary: &right.0.JSONDictionary)
9088
}
9189
}
9290

@@ -96,9 +94,9 @@ public func <- <T, Transform: TransformType where Transform.Object == T>(inout l
9694
*/
9795
public func <- <T: Mappable>(inout left: T, right: Map) {
9896
if right.mappingType == MappingType.fromJSON {
99-
FromJSON().object(&left, object: right.currentValue)
97+
FromJSON.object(&left, object: right.currentValue)
10098
} else {
101-
ToJSON().object(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
99+
ToJSON.object(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
102100
}
103101
}
104102

@@ -107,9 +105,9 @@ public func <- <T: Mappable>(inout left: T, right: Map) {
107105
*/
108106
public func <- <T: Mappable>(inout left: T?, right: Map) {
109107
if right.mappingType == MappingType.fromJSON {
110-
FromJSON().optionalObject(&left, object: right.currentValue)
108+
FromJSON.optionalObject(&left, object: right.currentValue)
111109
} else {
112-
ToJSON().optionalObject(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
110+
ToJSON.optionalObject(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
113111
}
114112
}
115113

@@ -118,9 +116,9 @@ public func <- <T: Mappable>(inout left: T?, right: Map) {
118116
*/
119117
public func <- <T: Mappable>(inout left: T!, right: Map) {
120118
if right.mappingType == MappingType.fromJSON {
121-
FromJSON().optionalObject(&left, object: right.currentValue)
119+
FromJSON.optionalObject(&left, object: right.currentValue)
122120
} else {
123-
ToJSON().optionalObject(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
121+
ToJSON.optionalObject(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
124122
}
125123
}
126124

@@ -130,9 +128,9 @@ public func <- <T: Mappable>(inout left: T!, right: Map) {
130128
*/
131129
public func <- <T: Mappable>(inout left: Dictionary<String, T>, right: Map) {
132130
if right.mappingType == MappingType.fromJSON {
133-
FromJSON().objectDictionary(&left, object: right.currentValue)
131+
FromJSON.objectDictionary(&left, object: right.currentValue)
134132
} else {
135-
ToJSON().objectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
133+
ToJSON.objectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
136134
}
137135
}
138136

@@ -141,9 +139,9 @@ public func <- <T: Mappable>(inout left: Dictionary<String, T>, right: Map) {
141139
*/
142140
public func <- <T: Mappable>(inout left: Dictionary<String, T>?, right: Map) {
143141
if right.mappingType == MappingType.fromJSON {
144-
FromJSON().optionalObjectDictionary(&left, object: right.currentValue)
142+
FromJSON.optionalObjectDictionary(&left, object: right.currentValue)
145143
} else {
146-
ToJSON().optionalObjectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
144+
ToJSON.optionalObjectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
147145
}
148146
}
149147

@@ -152,9 +150,9 @@ public func <- <T: Mappable>(inout left: Dictionary<String, T>?, right: Map) {
152150
*/
153151
public func <- <T: Mappable>(inout left: Dictionary<String, T>!, right: Map) {
154152
if right.mappingType == MappingType.fromJSON {
155-
FromJSON().optionalObjectDictionary(&left, object: right.currentValue)
153+
FromJSON.optionalObjectDictionary(&left, object: right.currentValue)
156154
} else {
157-
ToJSON().optionalObjectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
155+
ToJSON.optionalObjectDictionary(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
158156
}
159157
}
160158

@@ -164,9 +162,9 @@ public func <- <T: Mappable>(inout left: Dictionary<String, T>!, right: Map) {
164162
*/
165163
public func <- <T: Mappable>(inout left: Array<T>, right: Map) {
166164
if right.mappingType == MappingType.fromJSON {
167-
FromJSON().objectArray(&left, object: right.currentValue)
165+
FromJSON.objectArray(&left, object: right.currentValue)
168166
} else {
169-
ToJSON().objectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
167+
ToJSON.objectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
170168
}
171169
}
172170

@@ -175,9 +173,9 @@ public func <- <T: Mappable>(inout left: Array<T>, right: Map) {
175173
*/
176174
public func <- <T: Mappable>(inout left: Array<T>?, right: Map) {
177175
if right.mappingType == MappingType.fromJSON {
178-
FromJSON().optionalObjectArray(&left, object: right.currentValue)
176+
FromJSON.optionalObjectArray(&left, object: right.currentValue)
179177
} else {
180-
ToJSON().optionalObjectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
178+
ToJSON.optionalObjectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
181179
}
182180
}
183181

@@ -186,8 +184,8 @@ public func <- <T: Mappable>(inout left: Array<T>?, right: Map) {
186184
*/
187185
public func <- <T: Mappable>(inout left: Array<T>!, right: Map) {
188186
if right.mappingType == MappingType.fromJSON {
189-
FromJSON().optionalObjectArray(&left, object: right.currentValue)
187+
FromJSON.optionalObjectArray(&left, object: right.currentValue)
190188
} else {
191-
ToJSON().optionalObjectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
189+
ToJSON.optionalObjectArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
192190
}
193191
}

ObjectMapper/Core/ToJSON.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright (c) 2014 hearst. All rights reserved.
77
//
88

9-
import Foundation
9+
import class Foundation.NSNumber
1010

1111
private func setValue(value: AnyObject, forKey key: String, inout #dictionary: [String : AnyObject]) {
1212
return setValue(value, forKeyPathComponents: key.componentsSeparatedByString("."), dictionary: &dictionary)
@@ -31,9 +31,9 @@ private func setValue(value: AnyObject, forKeyPathComponents components: [String
3131
}
3232
}
3333

34-
class ToJSON {
34+
internal final class ToJSON {
3535

36-
func basicType<N>(field: N, key: String, inout dictionary: [String : AnyObject]) {
36+
class func basicType<N>(field: N, key: String, inout dictionary: [String : AnyObject]) {
3737
switch field {
3838
// basic Types
3939
case let x as NSNumber:
@@ -86,45 +86,45 @@ class ToJSON {
8686
}
8787
}
8888

89-
func optionalBasicType<N>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
89+
class func optionalBasicType<N>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
9090
if let field = field {
9191
basicType(field, key: key, dictionary: &dictionary)
9292
}
9393
}
9494

95-
func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
95+
class func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
9696
setValue(Mapper().toJSON(field), forKey: key, dictionary: &dictionary)
9797
}
9898

99-
func optionalObject<N: Mappable>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
99+
class func optionalObject<N: Mappable>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
100100
if let field = field {
101101
object(field, key: key, dictionary: &dictionary)
102102
}
103103
}
104104

105-
func objectArray<N: Mappable>(field: Array<N>, key: String, inout dictionary: [String : AnyObject]) {
105+
class func objectArray<N: Mappable>(field: Array<N>, key: String, inout dictionary: [String : AnyObject]) {
106106
let JSONObjects = Mapper().toJSONArray(field)
107107

108108
if !JSONObjects.isEmpty {
109109
setValue(JSONObjects, forKey: key, dictionary: &dictionary)
110110
}
111111
}
112112

113-
func optionalObjectArray<N: Mappable>(field: Array<N>?, key: String, inout dictionary: [String : AnyObject]) {
113+
class func optionalObjectArray<N: Mappable>(field: Array<N>?, key: String, inout dictionary: [String : AnyObject]) {
114114
if let field = field {
115115
objectArray(field, key: key, dictionary: &dictionary)
116116
}
117117
}
118118

119-
func objectDictionary<N: Mappable>(field: Dictionary<String, N>, key: String, inout dictionary: [String : AnyObject]) {
119+
class func objectDictionary<N: Mappable>(field: Dictionary<String, N>, key: String, inout dictionary: [String : AnyObject]) {
120120
let JSONObjects = Mapper().toJSONDictionary(field)
121121

122122
if !JSONObjects.isEmpty {
123123
setValue(JSONObjects, forKey: key, dictionary: &dictionary)
124124
}
125125
}
126126

127-
func optionalObjectDictionary<N: Mappable>(field: Dictionary<String, N>?, key: String, inout dictionary: [String : AnyObject]) {
127+
class func optionalObjectDictionary<N: Mappable>(field: Dictionary<String, N>?, key: String, inout dictionary: [String : AnyObject]) {
128128
if let field = field {
129129
objectDictionary(field, key: key, dictionary: &dictionary)
130130
}

0 commit comments

Comments
 (0)