Swift对Bool有任何内置的反向功能吗?

时间:2020-12-06 04:18:56

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.

以下扩展工作,但我想知道Swift是否有任何开箱即用的功能,这样做反向。我已经命令点击Bool,它没有任何反转,也没有在文档中看到任何内容。

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

5 个解决方案

#1


16  

! 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:

在Swift 3中,此运算符被定义为Bool类型的静态函数:

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).

请注意,在Swift中,变异方法通常不会返回新值(比较数组的sort()和sorted())。


Update: The proprosal

更新:proprosal

has been accepted, and a future version of Swift will have a toggle() method in the standard library:

已被接受,未来版本的Swift将在标准库中使用toggle()方法:

extension Bool {
  /// Equivalent to `someBool = !someBool`
  ///
  /// Useful when operating on long chains:
  ///
  ///    myVar.prop1.prop2.enabled.toggle()
  mutating func toggle() {
    self = !self
  }
}

#2


2  

Swift 4.2

As pointed out by Martin previously, the toggle function has finally arrived

正如Martin先前所指出的那样,切换功能终于到来了

So now, you can simply write

所以现在,你可以简单地写

bool.toggle()

and that would give you the intended results.

这会给你预期的结果。

#3


1  

Before you made the edit, your function was equivalent to the trivial assignment x = false;

在进行编辑之前,您的函数等同于普通赋值x = false;

But the way you now have it can be simplified to x = !x;. (That is true maps to false and false maps to true.)

但是你现在拥有它的方式可以简化为x =!x;。 (这是真的映射到false和false映射为true。)

(Note that the cool cats in C might even use x ^= 1, to toggle the least significant bit.)

(注意C中的酷猫甚至可能使用x ^ = 1来切换最低位。)

#4


1  

one of the best way and simpliest way to do this is to check the bool is equal to false as you set it like this:

最好的方法之一和最简单的方法是检查bool是否等于false,因为你这样设置:

 var mybool = true
 mybool = mybool == false

mybool will then always be the oposite of what it was before

然后mybool将永远是以前的对立面

#5


-1  

For above swift 4.2

对于上面的快速4.2

var isVisible = true
print(isVisible.toggle())  // false

for else

isVisible = !isVisible

#1


16  

! 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:

在Swift 3中,此运算符被定义为Bool类型的静态函数:

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).

请注意,在Swift中,变异方法通常不会返回新值(比较数组的sort()和sorted())。


Update: The proprosal

更新:proprosal

has been accepted, and a future version of Swift will have a toggle() method in the standard library:

已被接受,未来版本的Swift将在标准库中使用toggle()方法:

extension Bool {
  /// Equivalent to `someBool = !someBool`
  ///
  /// Useful when operating on long chains:
  ///
  ///    myVar.prop1.prop2.enabled.toggle()
  mutating func toggle() {
    self = !self
  }
}

#2


2  

Swift 4.2

As pointed out by Martin previously, the toggle function has finally arrived

正如Martin先前所指出的那样,切换功能终于到来了

So now, you can simply write

所以现在,你可以简单地写

bool.toggle()

and that would give you the intended results.

这会给你预期的结果。

#3


1  

Before you made the edit, your function was equivalent to the trivial assignment x = false;

在进行编辑之前,您的函数等同于普通赋值x = false;

But the way you now have it can be simplified to x = !x;. (That is true maps to false and false maps to true.)

但是你现在拥有它的方式可以简化为x =!x;。 (这是真的映射到false和false映射为true。)

(Note that the cool cats in C might even use x ^= 1, to toggle the least significant bit.)

(注意C中的酷猫甚至可能使用x ^ = 1来切换最低位。)

#4


1  

one of the best way and simpliest way to do this is to check the bool is equal to false as you set it like this:

最好的方法之一和最简单的方法是检查bool是否等于false,因为你这样设置:

 var mybool = true
 mybool = mybool == false

mybool will then always be the oposite of what it was before

然后mybool将永远是以前的对立面

#5


-1  

For above swift 4.2

对于上面的快速4.2

var isVisible = true
print(isVisible.toggle())  // false

for else

isVisible = !isVisible