Bool

struct Bool

A value type whose instances are either true or false.

Bool represents Boolean values in Swift. Create instances of Bool by using one of the Boolean literals true or 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 in other programming languages, in Swift, integers and strings cannot be used where a Boolean value is required.

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
}

Using Imported Boolean values

The C bool and Boolean types and the Objective-C BOOL type are all bridged into Swift as Bool. The single Bool type in Swift guarantees that functions, methods, and properties imported from C and Objective-C have a consistent type interface.

Inheritance CVarArg, Codable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, Equatable, ExpressibleByBooleanLiteral, Hashable, LosslessStringConvertible View Protocol Hierarchy →
Import import Swift

Initializers

init()

Creates an instance initialized to false.

Do not call this initializer directly. Instead, use the Boolean literal false to create a new Bool instance.

Declaration

init()
init(_: Bool)

Creates an instance equal to the given Boolean value.

value: The Boolean value to copy.

Declaration

init(_ value: Bool)
init(_: NSNumber)

[Foundation]

Declaration

init(_ number: NSNumber)
init(booleanLiteral:)

Creates an instance initialized to the specified Boolean literal.

Do not call this initializer directly. It is used by the compiler when you use a Boolean literal. Instead, create a new Bool instance by using one of the Boolean literals true or false.

var printedMessage = false

if !printedMessage {
    print("You look nice today!")
    printedMessage = true
}
// Prints "You look nice today!"

In this example, both assignments to the printedMessage variable call this Boolean literal initializer behind the scenes.

value: The value of the new instance.

Declaration

init(booleanLiteral value: Bool)
init(from:)

Creates a new instance by decoding from the given decoder.

This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.

decoder: The decoder to read data from.

Declaration

init(from decoder: Decoder)
init?(_:)

Creates a new Boolean value from the given string.

If the description value is any string other than "true" or "false", the result is nil. This initializer is case sensitive.

description: A string representation of the Boolean value.

Declaration

init?(_ description: String)

Instance Variables

var customMirror: Mirror

A mirror that reflects the Bool instance.

Declaration

var customMirror: Mirror { get }
var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for the Bool instance.

Deprecated: Bool.customPlaygroundQuickLook will be removed in a future Swift version.

Declaration

var customPlaygroundQuickLook: PlaygroundQuickLook { get }
var description: String

A textual representation of the Boolean value.

Declaration

var description: String { get }
var hashValue: Int

The hash value for the Boolean value.

Two values that are equal always have equal hash values.

Note: The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.

Declaration

var hashValue: Int { get }

Static Methods

static func random()

Returns a random Boolean value.

This method returns true and false with equal probability.

let flippedHeads = Boolean.random()
if flippedHeads {
    print("Heads, you win!")
} else {
    print("Maybe another try?")
}

Bool.random() uses the default random generator, Random.default. The call in the example above is equivalent to Bool.random(using: &Random.default).

Returns: Either true or false, randomly chosen with equal probability.

Declaration

static func random() -> Bool
static func random(using:)

Returns a random Boolean value, using the given generator as a source for randomness.

This method returns true and false with equal probability. Use this method to generate a random Boolean value when you are using a custom random number generator.

let flippedHeads = Boolean.random(using: &myGenerator)
if flippedHeads {
    print("Heads, you win!")
} else {
    print("Maybe another try?")
}

generator: The random number generator to use when creating the new random value. Returns: Either true or false, randomly chosen with equal probability.

Declaration

static func random<T>(using generator: inout T) -> Bool where T : RandomNumberGenerator

Instance Methods

func encode(to:)

Encodes this value into the given encoder.

This function throws an error if any values are invalid for the given encoder's format.

encoder: The encoder to write data to.

Declaration

func encode(to encoder: Encoder) throws
func hash(into:)

Hashes the essential components of this value by feeding them into the given hasher.

hasher: The hasher to use when combining the components of this instance.

Declaration

func hash(into hasher: inout Hasher)
mutating func toggle()

Declaration

mutating func toggle()