Skip to content

Commit

Permalink
Match pre-swift3 behavior for String -> Bool
Browse files Browse the repository at this point in the history
Before the Swift 3 update, boolValue used the NSString boolvalue
property. This property would return true for 'Y','y','T','t' in
addition to 'true'.

See more at
https://developer.apple.com/reference/foundation/nsstring/1409420-boolvalue

During the swift 3 upgrade, this was changed slightly so only "true"
would return true. This commit updates that behavior to match the
previous behavior for projects relying on that functionality.
  • Loading branch information
aterris committed Oct 14, 2016
1 parent 7075164 commit a544199
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ extension JSON { // : Swift.Bool
case .number:
return self.rawNumber.boolValue
case .string:
return self.rawString.caseInsensitiveCompare("true") == .orderedSame
return ["true", "y", "t"].contains() { (truthyString) in
return self.rawString.caseInsensitiveCompare(truthyString) == .orderedSame
}
default:
return false
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ class StringTests: XCTestCase {
XCTAssertEqual(json.URL!, URL(string:"http://github.com")!)
}

func testBool() {
let json = JSON("true")
XCTAssertTrue(json.boolValue)
}

func testBoolWithY() {
let json = JSON("Y")
XCTAssertTrue(json.boolValue)
}

func testBoolWithT() {
let json = JSON("T")
XCTAssertTrue(json.boolValue)
}

func testURLPercentEscapes() {
let emDash = "\\u2014"
let urlString = "http://examble.com/unencoded" + emDash + "string"
Expand Down

0 comments on commit a544199

Please sign in to comment.