Skip to content

Commit

Permalink
Merge pull request #509 from jamesjn/exists-for-wrong-type-and-out-of…
Browse files Browse the repository at this point in the history
…-bounds

Allow exists method to handle out of bounds and wrong type errors
  • Loading branch information
zhigang1992 committed Sep 26, 2016
2 parents 7ee9aab + 78fb831 commit eee5bd9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,10 @@ extension JSON {
}
}
public func exists() -> Bool{
if let errorValue = error , errorValue.code == ErrorNotExist{
return false
if let errorValue = error, errorValue.code == ErrorNotExist ||
errorValue.code == ErrorIndexOutOfBounds ||
errorValue.code == ErrorWrongType {
return false
}
return true
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/BaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,14 @@ class BaseTests: XCTestCase {
let dictionary = ["number":1111]
let json = JSON(dictionary)
XCTAssertFalse(json["unspecifiedValue"].exists())
XCTAssertFalse(json[0].exists())
XCTAssertTrue(json["number"].exists())

let array = [["number":1111]]
let jsonForArray = JSON(array)
XCTAssertTrue(jsonForArray[0].exists())
XCTAssertFalse(jsonForArray[1].exists())
XCTAssertFalse(jsonForArray["someValue"].exists())
}

func testErrorHandle() {
Expand Down

0 comments on commit eee5bd9

Please sign in to comment.