The following extension works, yet I was wondering if Swift has any out of the box function that does such reverse. I already command clicked on Bool and it doesn't have anything reverse like nor I saw anything in the Documentation.
var x = true
extension Bool{
mutating func reverse() -> Bool{
if self == true {
self = false
return self
} else {
self = true
return self
}
}
}
print(x.reverse()) // false
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. Returns a Boolean value indicating whether two values are equal. Toggles the Boolean variable’s value.
static func ! (Bool) -> Bool init? (String) A value type whose instances are either true or false. 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.
Here, the data type of num1 and num2 is Int, so we have passed integer values 3 and 4 during a function call. A Swift function may or may not return a value. If we want our function to return some value, we can use the return statement. For example, Here, we are returning the variable sum.
The correct approach in Swift is to compare the i value with zero in the while statement. 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.
!
is the "logical not" operator:
var x = true
x = !x
print(x) // false
In Swift 3 this operator is defined as a static function of the Bool
type:
public struct Bool {
// ...
/// 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 {
/// print("You look nice today!")
/// printedMessage = true
/// }
/// // Prints "You look nice today!"
///
/// - Parameter a: The Boolean value to negate.
prefix public static func !(a: Bool) -> Bool
// ...
}
There is no built-in mutating method which negates a boolean,
but you can implement it using the !
operator:
extension Bool {
mutating func negate() {
self = !self
}
}
var x = true
x.negate()
print(x) // false
Note that in Swift, mutating methods usually do not return the new
value (compare sort()
vs. sorted()
for arrays).
Update: The proprosal
toggle
to Bool
has been accepted, and a future version of Swift will have a
toggle()
method in the standard library:
extension Bool {
/// Equivalent to `someBool = !someBool`
///
/// Useful when operating on long chains:
///
/// myVar.prop1.prop2.enabled.toggle()
mutating func toggle() {
self = !self
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With