Skip to content

Commit 42f3e94

Browse files
committed
swift 2.2 cleanup
1 parent 732be87 commit 42f3e94

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

JSONJoy.xcodeproj/xcshareddata/xcschemes/JSONJoy.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0730"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

JSONJoy.xcodeproj/xcshareddata/xcschemes/JSONJoyOSX.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0730"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

JSONJoy.xcodeproj/xcshareddata/xcschemes/JSONJoyTests.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0730"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

README.md

+24-22
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ We want to translate that JSON to these Swift objects:
3636

3737
```swift
3838
struct Address {
39-
var objID: Int?
40-
var streetAddress: String?
41-
var city: String?
42-
var state: String?
43-
var postalCode: String?
39+
let objID: Int?
40+
let streetAddress: String?
41+
let city: String?
42+
let state: String?
43+
let postalCode: String?
4444
init() {
4545

4646
}
4747
}
4848

4949
struct User {
50-
var objID: Int?
51-
var firstName: String?
52-
var lastName: String?
53-
var age: Int?
54-
var address = Address()
50+
let objID: Int?
51+
let firstName: String?
52+
let lastName: String?
53+
let age: Int?
54+
let address = Address()
5555
init() {
5656

5757
}
@@ -80,11 +80,11 @@ JSONJoy makes this much simpler. We have our Swift objects implement the JSONJoy
8080

8181
```swift
8282
struct Address : JSONJoy {
83-
var objID: Int
84-
var streetAddress: String
85-
var city: String
86-
var state: String
87-
var postalCode: String
83+
let objID: Int
84+
let streetAddress: String
85+
let city: String
86+
let state: String
87+
let postalCode: String
8888

8989
init(_ decoder: JSONDecoder) throws {
9090
objID = try decoder["id"].getInt()
@@ -96,11 +96,11 @@ struct Address : JSONJoy {
9696
}
9797

9898
struct User : JSONJoy {
99-
var objID: Int
100-
var firstName: String
101-
var lastName: String
102-
var age: Int
103-
var address: Address
99+
let objID: Int
100+
let firstName: String
101+
let lastName: String
102+
let age: Int
103+
let address: Address
104104

105105
init(_ decoder: JSONDecoder) throws {
106106
objID = try decoder["id"].getInt()
@@ -156,13 +156,15 @@ firstName = decoder[5]["wrongKey"]["MoreWrong"].string
156156

157157
```swift
158158
struct Addresses : JSONJoy {
159-
var addresses = [Address]()
159+
let addresses: [Address]
160160

161161
init(_ decoder: JSONDecoder) {
162162
guard let addrs = decoder["addresses"].array else {throw JSONError.WrongType}
163+
var collect = [Address]()
163164
for addrDecoder in addrs {
164-
addresses.append(Address(addrDecoder))
165+
collect.append(Address(addrDecoder))
165166
}
167+
addresses = collect
166168
}
167169
}
168170
```

Source/JSONJoy.swift

+18-7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public class JSONDecoder {
114114
return i
115115
}
116116

117+
//get the bool and have it throw if it doesn't work
118+
public func getBool() throws -> Bool {
119+
if let _ = value as? NSNull {
120+
throw JSONError.WrongType
121+
}
122+
return bool
123+
}
124+
117125
//pull the raw values out of an array
118126
public func getArray<T>(inout collect: Array<T>?) {
119127
if let array = value as? Array<JSONDecoder> {
@@ -141,8 +149,11 @@ public class JSONDecoder {
141149
}
142150
}
143151
///the init that converts everything to something nice
144-
public init(_ raw: AnyObject) {
152+
public init(_ raw: AnyObject, isSub: Bool = false) {
145153
var rawObject: AnyObject = raw
154+
if let str = rawObject as? String where !isSub {
155+
rawObject = str.dataUsingEncoding(NSUTF8StringEncoding)!
156+
}
146157
if let data = rawObject as? NSData {
147158
var response: AnyObject?
148159
do {
@@ -157,13 +168,13 @@ public class JSONDecoder {
157168
if let array = rawObject as? NSArray {
158169
var collect = [JSONDecoder]()
159170
for val: AnyObject in array {
160-
collect.append(JSONDecoder(val))
171+
collect.append(JSONDecoder(val, isSub: true))
161172
}
162173
value = collect
163174
} else if let dict = rawObject as? NSDictionary {
164175
var collect = Dictionary<String,JSONDecoder>()
165176
for (key,val) in dict {
166-
collect[key as! String] = JSONDecoder(val)
177+
collect[key as! String] = JSONDecoder(val, isSub: true)
167178
}
168179
value = collect
169180
} else {
@@ -214,13 +225,13 @@ public class JSONDecoder {
214225
str.removeAtIndex(str.endIndex.advancedBy(-1))
215226
return str + "}"
216227
}
217-
if value != nil {
218-
if let _ = self.string {
219-
return "\"\(value!)\""
228+
if let v = value {
229+
if let s = self.string {
230+
return "\"\(s)\""
220231
} else if let _ = value as? NSNull {
221232
return "null"
222233
}
223-
return "\(value!)"
234+
return "\(v)"
224235
}
225236
return ""
226237
}

0 commit comments

Comments
 (0)