struct
Bool
Inheritance |
CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, Equatable, ExpressibleByBooleanLiteral, Hashable, LosslessStringConvertible
View Protocol Hierarchy →
|
---|---|
Import |
|
Initializers
Creates an instance initialized to false
.
Don't call this initializer directly. Instead, use the Boolean literal
false
to create a new Bool
instance.
Declaration
init
()
Declaration
init
(
_
value
:
Bool
)
[Foundation]
Declaration
init
(
_
number
:
NSNumber
)
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
and 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.
value
: The value of the new instance.
Declaration
init
(
booleanLiteral
value
:
Bool
)
Instantiates an instance of the conforming type from a string representation.
Declaration
init
?(
_
description
:
String
)
Instance Variables
A custom playground Quick Look for this instance.
If this type has value semantics, the PlaygroundQuickLook
instance
should be unaffected by subsequent mutations.
Declaration
var
customPlaygroundQuickLook
:
PlaygroundQuickLook
{
get
}
The hash value for the Boolean value.
Two values that are equal always have equal hash values.
Note: The hash value is not guaranteed to be stable across different
invocations of the same program. Do not persist the hash value across
program runs.
See Also: Hashable
Declaration
var
hashValue
:
Int
{
get
}
Instance 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!"
a
: The Boolean value to negate.
Declaration
Performs a logical AND operation on two Bool values.
The logical AND operator (&&
) combines two Bool 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:
- When
measurements.count
is equal to zero,lhs
evaluates tofalse
andrhs
is not evaluated, preventing a divide-by-zero error in the expressionsum / Double(measurements.count)
. The result of the operation isfalse
. - When
measurements.count
is greater than zero,lhs
evaluates totrue
andrhs
is evaluated. The result of evaluatingrhs
is the result of the&&
operation.
Parameters: lhs: The left-hand side of the operation. rhs: The right-hand side of the operation.
Declaration
Performs a logical OR operation on two Bool values.
The logical OR operator (||
) combines two Bool 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:
- When
error
is an empty string,lhs
evaluates totrue
andrhs
is not evaluated, skipping the call tomajorErrors.contains(_:)
. The result of the operation istrue
. - When
error
is not an empty string,lhs
evaluates tofalse
andrhs
is evaluated. The result of evaluatingrhs
is the result of the||
operation.
Parameters: lhs: The left-hand side of the operation. rhs: The right-hand side of the operation.
A value type whose instances are either
true
orfalse
.Bool
represents Boolean values in Swift. Create instances ofBool
by using one of the Boolean literalstrue
andfalse
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 other programming languages, in Swift integers and strings cannot be used where a Boolean value is expected.
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.