Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Swift have any built-in reverse function for a Bool?

Tags:

swift

boolean

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
like image 629
mfaani Avatar asked Dec 30 '16 16:12

mfaani


People also ask

What is the use of Boolean in Swift?

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.

What is static Func() in Swift?

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.

How to return a value from a function in Swift?

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.

What is the correct way to compare two values in Swift?

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.


1 Answers

! 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

  • SE-0199 Adding 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
  }
}
like image 147
Martin R Avatar answered Oct 10 '22 13:10

Martin R