Structureswift 5.10.0Swift
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.
Type 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: any 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.
static func ! (a: Bool
) -> Bool Performs a logical NOT operation on a Boolean value.
static func && (lhs: Bool, rhs: @autoclosure () throws -> Bool
) rethrows -> Bool Performs a logical AND operation on two Boolean values.
static func == (lhs: Bool, rhs: Bool
) -> Bool static func || (lhs: Bool, rhs: @autoclosure () throws -> Bool
) rethrows -> Bool Performs a logical OR operation on two Boolean values.
Instance members
var customMirror: Mirror
A mirror that reflects the
Bool
instance.var description: String
A textual representation of the Boolean value.
func encode(to: any 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.
Show obsolete interfaces (1)
Hide obsolete interfaces
var customPlaygroundQuickLook: _PlaygroundQuickLook
A custom playground Quick Look for the
Bool
instance.
Type features
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
Available in Foundation
Type members
Show obsolete interfaces (1)
Hide obsolete interfaces
Extension in BSON
Conformances
Instance features
Extension in MultipartKit
Conformances
protocol MultipartPartConvertible
A protocol to provide custom behaviors for parsing and serializing types from and to multipart data.
Type members
Instance members
Extension in ArgumentParser
Conformances
protocol ExpressibleByArgument
A type that can be expressed as a command-line argument.
Type features
Instance features
Extension in SwiftASN1
Conformances
protocol BERImplicitlyTaggable
protocol BERParseable
Defines a type that can be parsed from a BER-encoded form, which is a superset of DER.
protocol BERSerializable
Defines a type that can be serialized in BER-encoded form.
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.
Type members
init(berEncoded: ASN1Node, withIdentifier: ASN1Identifier
) throws init(derEncoded: ASN1Node, withIdentifier: ASN1Identifier
) throws static var defaultIdentifier: ASN1Identifier
Instance members
Type 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.
init(berASN1Any: ASN1Any
) throws Construct this node from an ASN.1 ANY object.
init(berASN1Any: ASN1Any, withIdentifier: ASN1Identifier
) throws Construct this node from an ASN.1 ANY object.
init(berEncoded: inout ASN1NodeCollection.Iterator
) throws init(berEncoded: ASN1Node
) throws By default, uses the underlying DERParseable initializer.
init(berEncoded: [UInt8]
) throws Initialize this object from a serialized BER representation.
init(berEncoded: ArraySlice<UInt8>
) throws Initialize this object from a serialized BER representation.
init(berEncoded: ASN1Node
) throws Initialize this object from a serialized BER representation.
init(berEncoded: inout ASN1NodeCollection.Iterator, withIdentifier: ASN1Identifier
) throws Initialize this object as one element of a constructed ASN.1 object.
init(berEncoded: [UInt8], withIdentifier: ASN1Identifier
) throws Initialize this object from a serialized BER representation.
init(berEncoded: ArraySlice<UInt8>, withIdentifier: ASN1Identifier
) throws Initialize this object from a serialized BER representation.
init(derEncoded: inout ASN1NodeCollection.Iterator
) throws Initialize this object as one element of a constructed ASN.1 object.
init(derEncoded: [UInt8]
) throws Initialize this object from a serialized DER representation.
init(derEncoded: ArraySlice<UInt8>
) throws Initialize this object from a serialized DER representation.
init(derEncoded: ASN1Node
) throws init(derEncoded: inout ASN1NodeCollection.Iterator, withIdentifier: ASN1Identifier
) throws Initialize this object as one element of a constructed ASN.1 object.
init(derEncoded: [UInt8], withIdentifier: ASN1Identifier
) throws Initialize this object from a serialized DER representation.
init(derEncoded: ArraySlice<UInt8>, withIdentifier: ASN1Identifier
) throws Initialize this object from a serialized DER representation.
Instance features
Extension in Atomics
Conformances
protocol AtomicValue
A type that supports atomic operations through a separate atomic storage representation.
Types
Extension in MMIO
Conformances
Type members
Instance members
Extension in NIOConcurrencyHelpers
Conformances
Typealiases
Type 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
Extension in SwiftSyntaxBuilder
Conformances
protocol ExpressibleByLiteralSyntax
A Swift type whose value can be represented directly in source code by a Swift literal.
Instance 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
.
Type features
static var defaultContentType: HTTPMediaType
static func decodeRequest(Request
) -> EventLoopFuture<Self> static func decodeRequest(Request
) async throws -> Self
Instance features
func afterDecode(
) throws func beforeEncode(
) throws func encodeResponse(for: Request
) -> EventLoopFuture<Response> func encodeResponse(status: HTTPStatus, headers: HTTPHeaders, for: Request
) -> EventLoopFuture<Response> Asynchronously encodes
Self
into aResponse
, setting the supplied status and headers.
Extension in Yams
Conformances
protocol NodeRepresentable
Type is representable as
Node
.protocol ScalarConstructible
Types conforming to this protocol can be extracted
Node.Scalar
s.protocol ScalarRepresentable
Type is representable as
Node.scalar
.protocol YAMLEncodable
Types conforming to this protocol can be encoded by
YamlEncoder
.
Type members
static func construct(from: Node.Scalar
) -> Bool? Construct an instance of
Bool
, if possible, from the specified scalar.
Instance members
func represented(
) -> Node.Scalar This value’s
Node.scalar
representation.
Instance features
func box(
) -> Node Returns this value wrapped in a
Node.scalar
.