Skip to content

Commit

Permalink
Merge pull request #1797 from Kaiede/conditionalBridgeFor4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-ci committed Dec 11, 2018
2 parents 3d07e9a + 9042291 commit af89a49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Foundation/NSNumber.swift
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions TestFoundation/TestNSNumberBridging.swift
Expand Up @@ -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)
Expand Down

0 comments on commit af89a49

Please sign in to comment.