struct
Bool
A value type whose instances are either true
or false
.
Inheritance | Codable, CustomReflectable, CustomStringConvertible, Equatable, ExpressibleByBooleanLiteral, Hashable, LosslessStringConvertible |
---|
Initializers
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
public
init
()
Creates an instance equal to the given Boolean value.
- Parameter value: The Boolean value to copy.
Declaration
@
inlinable
public
init
(
_
value
:
Bool
)
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
{
(
"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.
- Parameter value: The value of the new instance.
Declaration
public
init
(
booleanLiteral
value
:
Bool
)
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.
- Parameter decoder: The decoder to read data from.
Declaration
public
init
(
from
decoder
:
Decoder
)
throws
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.
- Parameter description: A string representation of the Boolean value.
Declaration
@
inlinable
public
init
?(
_
description
:
String
)
Instance Variables
A custom playground Quick Look for the Bool
instance.
Declaration
var
customPlaygroundQuickLook
:
_PlaygroundQuickLook
Instance Methods
Encodes this value into the given encoder.
This function throws an error if any values are invalid for the given encoder's format.
- Parameter encoder: The encoder to write data to.
Declaration
public
func
encode
(
to
encoder
:
Encoder
)
throws
Hashes the essential components of this value by feeding them into the given hasher.
- Parameter hasher: The hasher to use when combining the components of this instance.
Declaration
@
inlinable
public
func
hash
(
into
hasher
:
inout
Hasher
)
Toggles the Boolean variable's value.
Use this method to toggle a Boolean value from true
to false
or from
false
to true
.
var
bools
= [
true
,
false
]
bools
[
0
].
toggle
()
// bools == [false, false]
Declaration
@
inlinable
public
mutating
func
toggle
()
Type Methods
Performs a logical NOT operation on a Boolean value.
The logical NOT operator (!
) inverts a Boolean value. If the value is
true
, the result of the operation is false
; if the value is false
,
the result is true
.
var
printedMessage
=
false
if
!
printedMessage
{
(
"You look nice today!"
)
printedMessage
=
true
}
// Prints "You look nice today!"
- Parameter a: The Boolean value to negate.
Declaration
Declaration
public
static
func
!=(
lhs
:
Self
,
rhs
:
Self
) -
>
Bool
Performs a logical AND operation on two Boolean values.
The logical AND operator (&&
) combines two Boolean values and returns
true
if both of the values are true
. If either of the values is
false
, the operator returns false
.
This operator uses short-circuit evaluation: The left-hand side (lhs
) is
evaluated first, and the right-hand side (rhs
) is evaluated only if
lhs
evaluates to true
. For example:
let
measurements
= [
7.44
,
6.51
,
4.74
,
5.88
,
6.27
,
6.12
,
7.76
]
let
sum
=
measurements
.
reduce
(
0
,
combine
: +)
if
measurements
.
count
>
0
&
&
sum
/
Double
(
measurements
.
count
)
<
6.5
{
(
"Average measurement is less than 6.5"
)
}
// Prints "Average measurement is less than 6.5"
In this example, lhs
tests whether measurements.count
is greater than
zero. Evaluation of the &&
operator is one of the following:
Declaration
Returns a random Boolean value.
This method returns true
and false
with equal probability.
let
flippedHeads
=
Bool
.
random
()
if
flippedHeads
{
(
"Heads, you win!"
)
}
else
{
(
"Maybe another try?"
)
}
This method is equivalent to calling Bool.random(using:)
, passing in
the system's default random generator.
Declaration
@
inlinable
public
static
func
random
() -
>
Bool
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
=
Bool
.
random
(
using
:
&
myGenerator
)
if
flippedHeads
{
(
"Heads, you win!"
)
}
else
{
(
"Maybe another try?"
)
}
Note: The algorithm used to create random values may change in a future version of Swift. If you're passing a generator that results in the same sequence of Boolean values each time you run your program, that sequence may change when your program is compiled using a different version of Swift.
- Parameter generator: The random number generator to use when creating the new random value.
Declaration
@
inlinable
public
static
func
random
<
T
>
(
using
generator
:
inout
T
) -
>
Bool
where
T
:
RandomNumberGenerator
Performs a logical OR operation on two Boolean values.
The logical OR operator (||
) combines two Boolean values and returns
true
if at least one of the values is true
. If both values are
false
, the operator returns false
.
This operator uses short-circuit evaluation: The left-hand side (lhs
) is
evaluated first, and the right-hand side (rhs
) is evaluated only if
lhs
evaluates to false
. For example:
let
majorErrors
:
Set
= [
"No first name"
,
"No last name"
, ...]
let
error
=
""
if
error
.
isEmpty
|| !
majorErrors
.
contains
(
error
) {
(
"No major errors detected"
)
}
else
{
(
"Major error: \(
error
)"
)
}
// Prints "No major errors detected"
In this example, lhs
tests whether error
is an empty string.
Evaluation of the ||
operator is one of the following:
Bool
represents Boolean values in Swift. Create instances ofBool
by using one of the Boolean literalstrue
orfalse
, or by assigning the result of a Boolean method or operation to a variable or constant.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:The correct approach in Swift is to compare the
i
value with zero in thewhile
statement.Using Imported Boolean values
The C
bool
andBoolean
types and the Objective-CBOOL
type are all bridged into Swift asBool
. The singleBool
type in Swift guarantees that functions, methods, and properties imported from C and Objective-C have a consistent type interface.