Skip to content

Commit 9042291

Browse files
committedDec 7, 2018
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.
1 parent 3d07e9a commit 9042291

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed
 

‎Foundation/NSNumber.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,16 @@ extension Bool : _ObjectiveCBridgeable {
541541
}
542542

543543
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Bool?) -> Bool {
544-
if x.intValue == 0 || x.intValue == 1 {
545-
result = x.boolValue
544+
if x === kCFBooleanTrue || NSNumber(value: 1) == x {
545+
result = true
546+
return true
547+
} else if x === kCFBooleanFalse || NSNumber(value: 0) == x {
548+
result = false
546549
return true
547-
} else {
548-
result = nil
549-
return false
550550
}
551+
552+
result = nil
553+
return false
551554
}
552555

553556
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNumber?) -> Bool {

‎TestFoundation/TestNSNumberBridging.swift

+10
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,16 @@ class TestNSNumberBridging : XCTestCase {
642642
XCTAssertNotNil(b3)
643643
XCTAssertEqual(b3, true)
644644

645+
let b4 = NSNumber(value: 0.0) as? Bool
646+
XCTAssertNotNil(b4)
647+
XCTAssertEqual(b4, false)
648+
649+
let b5 = NSNumber(value: 1.0) as? Bool
650+
XCTAssertNotNil(b5)
651+
XCTAssertEqual(b5, true)
652+
653+
XCTAssertNil(NSNumber(value: 0.25) as? Bool)
654+
XCTAssertNil(NSNumber(value: 1.25) as? Bool)
645655
XCTAssertNil(NSNumber(value: -1) as? Bool)
646656
XCTAssertNil(NSNumber(value: 2) as? Bool)
647657
XCTAssertNil(NSNumber(value: Int8.min) as? Bool)

0 commit comments

Comments
 (0)
Please sign in to comment.