Skip to content

Commit bf8eed4

Browse files
committed
Add check for null value using hasValue; rename isNil to hasKey.
Squashed commit of the following: commit dca6ef1 Merge: 3fe4c28 449f8ff Author: Ben Kreeger <ben.kreeger@icloud.com> Date: Mon Oct 12 14:29:51 2015 -0500 Merge branch 'feature/is-null' of github.com:kreeger/ModelRocket into feature/is-null commit 3fe4c28 Author: Ben Kreeger <ben.kreeger@icloud.com> Date: Mon Oct 12 14:29:21 2015 -0500 Alter new method to be hasValue, with isNil as hasKey, too. commit 449f8ff Author: Ben Kreeger <ben.kreeger@icloud.com> Date: Fri Oct 9 11:17:41 2015 -0500 Change spaces to a tab. commit 499dafd Author: Ben Kreeger <ben.kreeger@icloud.com> Date: Fri Oct 9 11:13:50 2015 -0500 Add ability to check for `null` in JSON with `is NSNull`.
1 parent 1978ec9 commit bf8eed4

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

ModelRocket/JSON.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ public struct JSON {
7070
}
7171
}
7272

73-
public var isNil: Bool {
73+
@available(*, deprecated=1.2, message="Use !hasKey instead.") public var isNil: Bool {
7474
return (object == nil)
7575
}
76+
77+
public var hasKey: Bool {
78+
return object != nil
79+
}
80+
81+
public var hasValue: Bool {
82+
return !(object is NSNull)
83+
}
7684
}
7785

7886
// MARK: - CustomStringConvertible
@@ -285,6 +293,11 @@ extension Dictionary {
285293
}
286294
}
287295

296+
// MARK: - NSNull
297+
298+
extension JSON {
299+
}
300+
288301
// MARK: - Equatable
289302

290303
extension JSON: Equatable {}

ModelRocketTests/JSONTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JSONTests: XCTestCase {
5555

5656
// No data
5757
let emptyJSON = JSON(data: nil)
58-
XCTAssertTrue(emptyJSON.isNil, "JSON not nil")
58+
XCTAssertFalse(emptyJSON.hasKey, "JSON not nil")
5959

6060
// String
6161
XCTAssertEqual(vehicleJSON["make"].stringValue, "BMW")
@@ -436,6 +436,16 @@ class JSONTests: XCTestCase {
436436
XCTAssertEqual(json["array"][0].intValue, 1)
437437
}
438438

439+
// MARK: - NSNull (from loaded data)
440+
441+
func testNSNull() {
442+
let jsonPath = NSBundle(forClass: self.dynamicType).pathForResource("Tests", ofType: "json")
443+
let jsonData = NSData(contentsOfFile: jsonPath!)
444+
let json = JSON(data: jsonData)
445+
446+
XCTAssertFalse(json["driver"].hasValue)
447+
}
448+
439449
// MARK: - Dictionary
440450

441451
func testDictionaryLiteralConvertible() {

ModelRocketTests/Tests.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"number_of_doors" : 4,
77
"zero_to_sixty_time" : 4.7,
88
"nav_system_standard" : true,
9+
"driver": null,
910
"manufacturer" : {
1011
"company_name" : "Bayerische Motoren Werke AG",
1112
"headquarters" : "Munich, Bavaria, Germany",
@@ -22,4 +23,4 @@
2223
"sedan" : 1024,
2324
"gran_coupe" : 512
2425
}
25-
}
26+
}

0 commit comments

Comments
 (0)