Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Bool in Swift is a fundamental value type that represents a logical entity, strictly restricted to one of two constant states: true or false. Unlike languages such as C or C++, Swift enforces strict type safety; it does not implicitly convert numeric values (like 0 or 1) or object references to boolean values. Under the hood, Bool is implemented as a structure (struct) in the Swift Standard Library. It conforms to several key protocols, including ExpressibleByBooleanLiteral, Equatable, Hashable, and LosslessStringConvertible.

Initialization and Syntax

A Bool can be instantiated using boolean literals. Swift’s type inference automatically resolves the type when a literal is assigned, though explicit type annotation is fully supported.
let inferredBool = true
let explicitBool: Bool = false
Because Bool conforms to LosslessStringConvertible, it can also be initialized from a string, returning an optional Bool? that resolves to nil if the string is not exactly "true" or "false".
let parsedTrue = Bool("true")   // Optional(true)
let parsedInvalid = Bool("yes") // nil

Logical Operators

Swift provides three standard logical operators for Bool manipulation. The binary operators (&& and ||) utilize short-circuit evaluation, meaning the right-hand operand is only evaluated if the left-hand operand does not definitively determine the overall expression’s outcome.
  • Logical NOT (!): A unary prefix operator that inverts the boolean value.
  • Logical AND (&&): A binary operator that returns true only if both operands are true.
  • Logical OR (||): A binary operator that returns true if at least one operand is true.
let operandA = true
let operandB = false

let negation = !operandA               // false
let conjunction = operandA && operandB // false
let disjunction = operandA || operandB // true

State Mutation

Because Bool is a value type, mutating its state requires the variable to be declared with var. The Swift Standard Library provides the toggle() method, which performs an in-place inversion of the boolean value. This is computationally equivalent to self = !self but is preferred for ergonomic reasons. It improves readability and avoids the repetition of long variable paths, though it provides no inherent thread-safety or atomic guarantees.
var state = false
state.toggle() // state is now true

// Ergonomic benefit avoids repetition:
// myObject.configuration.isActive.toggle()

Memory and Bridging

In memory, a Swift Bool occupies 1 byte (8 bits). When interoperating with C or Objective-C APIs, Swift’s compiler automatically bridges Bool to the corresponding environment types:
  • Objective-C: Bridged to ObjCBool (which resolves to signed char or bool depending on the architecture).
  • C: Bridged to CBool (a typealias for C’s stdbool.h bool).
Master Swift with Deep Grasping Methodology!Learn More