From a54419975c908dbafb55a10eba1bd62f01938e87 Mon Sep 17 00:00:00 2001 From: Andrew Terris Date: Fri, 14 Oct 2016 14:26:19 -0400 Subject: [PATCH] Match pre-swift3 behavior for String -> Bool 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. --- Source/SwiftyJSON.swift | 4 +++- Tests/StringTests.swift | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Source/SwiftyJSON.swift b/Source/SwiftyJSON.swift index 5d10d44b..7e71a187 100644 --- a/Source/SwiftyJSON.swift +++ b/Source/SwiftyJSON.swift @@ -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 } diff --git a/Tests/StringTests.swift b/Tests/StringTests.swift index 1d46eb88..fd787927 100644 --- a/Tests/StringTests.swift +++ b/Tests/StringTests.swift @@ -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"