Skip to content

Commit 78137e2

Browse files
committed
Merge pull request #755 from frootloops/bool
Simplifying the Bool creation with NSNumber
2 parents 5d6c20b + adccf23 commit 78137e2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ extension Bool: _ObjectiveCBridgeable {
313313
}
314314

315315
public init(_ number: NSNumber) {
316-
if number.boolValue { self = true }
317-
else { self = false }
316+
self = number.boolValue
318317
}
319318

320319
public static func _getObjectiveCType() -> Any.Type {

test/1_stdlib/BoolBridge.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-run-simple-swift
2+
// REQUIRES: executable_test
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
import StdlibUnittest
7+
8+
let BoolTests = TestSuite("Bool")
9+
10+
BoolTests.test("Init with NSNumber") {
11+
expectFalse(Bool(NSNumber(integerLiteral: 0)))
12+
expectTrue(Bool(NSNumber(integerLiteral: 1)))
13+
expectTrue(Bool(NSNumber(integerLiteral: 2)))
14+
}
15+
16+
runAllTests()

0 commit comments

Comments
 (0)