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
protocol CVarArg
A type whose instances can be encoded, and appropriately passed, as elements of a C
va_list
.protocol CustomReflectable
A type that explicitly supplies its own mirror.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol ExpressibleByBooleanLiteral
A type that can be initialized with the Boolean literals
true
andfalse
.protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol LosslessStringConvertible
A type that can be represented as a string in a lossless, unambiguous way.
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Members
init(
) Creates an instance initialized to
false
.init(Bool
) Creates an instance equal to the given Boolean value.
init?(String
) Creates a new Boolean value from the given string.
init(booleanLiteral: Bool
) Creates an instance initialized to the specified Boolean literal.
init(from: Decoder
) throws Creates a new instance by decoding from the given decoder.
static func random(
) -> Bool Returns a random Boolean value.
static func random<T>(using: inout T
) -> Bool Returns a random Boolean value, using the given generator as a source for randomness.
var customMirror: Mirror
A mirror that reflects the
Bool
instance.var description: String
A textual representation of the Boolean value.
static func ! (Bool
) -> Bool Performs a logical NOT operation on a Boolean value.
static func && (Bool, () throws -> Bool
) rethrows -> Bool Performs a logical AND operation on two Boolean values.
static func == (Bool, Bool
) -> Bool static func || (Bool, () throws -> Bool
) rethrows -> Bool Performs a logical OR operation on two Boolean values.
func encode(to: Encoder
) throws Encodes this value into the given encoder.
func hash(into: inout Hasher
) Hashes the essential components of this value by feeding them into the given hasher.
func toggle(
) Toggles the Boolean variable’s value.
var customPlaygroundQuickLook: _PlaygroundQuickLook
A custom playground Quick Look for the
Bool
instance.
Features
Available in Foundation
Members
Extension in BSON
Conformances
Features
Extension in MultipartKit
Conformances
Members
Extension in ArgumentParser
Conformances
protocol ExpressibleByArgument
A type that can be expressed as a command-line argument.
Extension in SwiftASN1
Conformances
protocol DERImplicitlyTaggable
An ASN.1 node that can tolerate having an implicit tag.
protocol DERParseable
Defines a type that can be parsed from a DER-encoded form.
protocol DERSerializable
Defines a type that can be serialized in DER-encoded form.
Members
init(derEncoded: ASN1Node, withIdentifier: ASN1Identifier
) throws static var defaultIdentifier: ASN1Identifier
func serialize(into: inout DER.Serializer, withIdentifier: ASN1Identifier
) throws
Features
init(asn1Any: ASN1Any
) throws Construct this node from an ASN.1 ANY object.
init(asn1Any: ASN1Any, withIdentifier: ASN1Identifier
) throws Construct this node from an ASN.1 ANY object.
Extension in Atomics
Conformances
protocol AtomicValue
A type that supports atomic operations through a separate atomic storage representation.
Members
Extension in JSONDecoding
Conformances
protocol JSONDecodable
A type that can be decoded from a JSON variant value.
Members
Extension in JSONEncoding
Conformances
protocol JSONEncodable
A type that can be encoded to a JSON variant value.
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
protocol BSONEncodable
A type that can be encoded to a BSON variant value.
Members
Extension in NIOConcurrencyHelpers
Conformances
protocol AtomicPrimitive
The protocol that all types that can be made atomic must conform to.
protocol NIOAtomicPrimitive
The protocol that all types that can be made atomic must conform to.
Members
static let atomic_add: (OpaquePointer, Bool) -> Bool
static let atomic_compare_and_exchange: (OpaquePointer, Bool, Bool) -> Bool
static let atomic_create: (Bool) -> OpaquePointer
static let atomic_destroy: (OpaquePointer) -> Void
static let atomic_exchange: (OpaquePointer, Bool) -> Bool
static let atomic_load: (OpaquePointer) -> Bool
static let atomic_store: (OpaquePointer, Bool) -> Void
static let atomic_sub: (OpaquePointer, Bool) -> Bool
static let nio_atomic_add: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool) -> Bool
static let nio_atomic_compare_and_exchange: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool, Bool) -> Bool
static let nio_atomic_create_with_existing_storage: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool) -> Void
static let nio_atomic_exchange: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool) -> Bool
static let nio_atomic_load: (UnsafeMutablePointer<catmc_nio_atomic__Bool>) -> Bool
static let nio_atomic_store: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool) -> Void
static let nio_atomic_sub: (UnsafeMutablePointer<catmc_nio_atomic__Bool>, Bool) -> Bool
typealias AtomicWrapper
Extension in SwiftSyntaxBuilder
Conformances
protocol ExpressibleByLiteralSyntax
A Swift type whose value can be represented directly in source code by a Swift literal.
Members
Extension in JSONDecoding
Conformances
protocol JSONDecodable
A type that can be decoded from a JSON variant value.
Members
Extension in JSONEncoding
Conformances
Members
Extension in Vapor
Conformances
protocol AsyncRequestDecodable
Can convert
Request
to aSelf
.protocol AsyncResponseEncodable
Can convert
self
to aResponse
.protocol Content
Convertible to / from content in an HTTP message.
protocol RequestDecodable
Can convert
Request
to aSelf
.protocol ResponseEncodable
Can convert
self
to aResponse
.
Features
func encodeResponse(status: HTTPStatus, headers: HTTPHeaders, for: Request
) -> EventLoopFuture<Response> Asynchronously encodes
Self
into aResponse
, setting the supplied status and headers.