From 904229111cd6c406d521d7134bd512fd7dbfba18 Mon Sep 17 00:00:00 2001 From: Adam Thayer Date: Sun, 23 Sep 2018 10:20:40 -0700 Subject: [PATCH] Fix Conditional Bridge Behavior for NSNumber to Bool The original change for this did not check the behavior. A better implementation here is to use the one from Swift itself, which also matches the initializer that this extension also adds. --- Foundation/NSNumber.swift | 13 ++++++++----- TestFoundation/TestNSNumberBridging.swift | 10 ++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Foundation/NSNumber.swift b/Foundation/NSNumber.swift index 704caa36a4..b693bbee5c 100644 --- a/Foundation/NSNumber.swift +++ b/Foundation/NSNumber.swift @@ -541,13 +541,16 @@ extension Bool : _ObjectiveCBridgeable { } public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) -> Bool { - if x.intValue == 0 || x.intValue == 1 { - result = x.boolValue + if x === kCFBooleanTrue || NSNumber(value: 1) == x { + result = true + return true + } else if x === kCFBooleanFalse || NSNumber(value: 0) == x { + result = false return true - } else { - result = nil - return false } + + result = nil + return false } public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Bool { diff --git a/TestFoundation/TestNSNumberBridging.swift b/TestFoundation/TestNSNumberBridging.swift index 3a84175227..ade7bca089 100644 --- a/TestFoundation/TestNSNumberBridging.swift +++ b/TestFoundation/TestNSNumberBridging.swift @@ -642,6 +642,16 @@ class TestNSNumberBridging : XCTestCase { XCTAssertNotNil(b3) XCTAssertEqual(b3, true) + let b4 = NSNumber(value: 0.0) as? Bool + XCTAssertNotNil(b4) + XCTAssertEqual(b4, false) + + let b5 = NSNumber(value: 1.0) as? Bool + XCTAssertNotNil(b5) + XCTAssertEqual(b5, true) + + XCTAssertNil(NSNumber(value: 0.25) as? Bool) + XCTAssertNil(NSNumber(value: 1.25) as? Bool) XCTAssertNil(NSNumber(value: -1) as? Bool) XCTAssertNil(NSNumber(value: 2) as? Bool) XCTAssertNil(NSNumber(value: Int8.min) as? Bool)