StructureSwift5.9.0

    Bool

    A value type whose instances are either true or false.

    @frozen struct Bool

    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
    }
    // error: Cannot convert value of type 'Int' to expected condition type 'Bool'

    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.

    Citizens in Swift

    Conformances

    Members

    Features

    Available in Foundation

    Members

    Extension in BSON

    Conformances

    Features

    Extension in MultipartKit

    Conformances

    Members

    Extension in ArgumentParser

    Conformances

    Extension in SwiftASN1

    Conformances

    Members

    Features

    Extension in Atomics

    Conformances

    • protocol AtomicValue

      A type that supports atomic operations through a separate atomic storage representation.

    Members

    Extension in JSONDecoding

    Conformances

    Members

    Extension in JSONEncoding

    Conformances

    Members

    Extension in BSONDecoding

    Conformances

    • protocol BSONDecodable

      A type that can be decoded from a BSON variant value backed by some type of storage not particular to the decoded type.

    Members

    Extension in BSONEncoding

    Conformances

    Members

    Extension in NIOConcurrencyHelpers

    Conformances

    Members

    Extension in SwiftSyntaxBuilder

    Conformances

    Members

    Extension in JSONDecoding

    Conformances

    Members

    Extension in JSONEncoding

    Conformances

    Members

    Extension in Vapor

    Conformances

    Features