Structure

Bool

A value type whose instances are either true or false.

Overview

Bool represents Boolean values in Swift. Create instances of Bool by using one of the Boolean literals true and false or by assigning the result of a Boolean method or operation to a variable or constant.

var godotHasArrived = false

let numbers = 1...5
let containsTen = numbers.contains(10)
print(containsTen)
// Prints "false"

let (a, b) == (100, 101)
let aFirst = a < b
print(aFirst)
// Prints "true"

Swift uses only simple Boolean values in conditional contexts to help avoid accidental programming errors and to help maintain the clarity of each control statement. Unlike other programming languages, in Swift integers and strings cannot be used where a Boolean value is expected.

For example, the following code sample does not compile, because it attempts to use the integer i in a logical context:

var i = 5
while i {
    print(i)
    i -= 1
}

The correct approach in Swift is to compare the i value with zero in the while statement.

while i != 0 {
    print(i)
    i -= 1
}

Symbols

Initializers

init()

Creates an instance initialized to false.

init(Bool)

Creates an instance representing the given logical value.

init(NSNumber)
init(booleanLiteral: Bool)

Creates an instance initialized to the specified Boolean literal.

init?(String)

Instance Properties

var description: String

A textual representation of the Boolean value.

var hashValue: Int

The hash value for the Boolean value.

var customMirror: Mirror

A mirror that reflects the Bool instance.

var customPlaygroundQuickLook: PlaygroundQuickLook

Operator Functions

static func !(Bool)

Performs a logical NOT operation on a Boolean value.

static func &&(Bool, () -> Bool)

Performs a logical AND operation on two Bool values.

static func ==(Bool, Bool)
static func ||(Bool, () -> Bool)

Performs a logical OR operation on two Bool values.