Skip to content

Commit eee5bd9

Browse files
authored
Merge pull request SwiftyJSON#509 from jamesjn/exists-for-wrong-type-and-out-of-bounds
Allow exists method to handle out of bounds and wrong type errors
2 parents 7ee9aab + 78fb831 commit eee5bd9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Source/SwiftyJSON.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,10 @@ extension JSON {
780780
}
781781
}
782782
public func exists() -> Bool{
783-
if let errorValue = error , errorValue.code == ErrorNotExist{
784-
return false
783+
if let errorValue = error, errorValue.code == ErrorNotExist ||
784+
errorValue.code == ErrorIndexOutOfBounds ||
785+
errorValue.code == ErrorWrongType {
786+
return false
785787
}
786788
return true
787789
}

Tests/BaseTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ class BaseTests: XCTestCase {
214214
let dictionary = ["number":1111]
215215
let json = JSON(dictionary)
216216
XCTAssertFalse(json["unspecifiedValue"].exists())
217+
XCTAssertFalse(json[0].exists())
217218
XCTAssertTrue(json["number"].exists())
219+
220+
let array = [["number":1111]]
221+
let jsonForArray = JSON(array)
222+
XCTAssertTrue(jsonForArray[0].exists())
223+
XCTAssertFalse(jsonForArray[1].exists())
224+
XCTAssertFalse(jsonForArray["someValue"].exists())
218225
}
219226

220227
func testErrorHandle() {

0 commit comments

Comments
 (0)