如果语句带有可选类型Bool

如果语句带有可选类型Bool

本文介绍了Swift良好的编码实践:如果语句带有可选类型Bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在Swift中开发一个应用程序,今天我花了近一个小时来调试一个完全出乎意料的问题。这一切都来自下面的代码。

So I've been developing an app in Swift, and today I spent nearly an hour debugging a problem that turned out to be completely unexpected. It all resulted from the code below.

if (hero.isAI) { //isAI is a Bool

}

问题是这个if语句总是返回true。所以我想也许我在某处将isAI设置为true但最后我意识到我将isAI声明为可选类型,如下所示。

The problem was that this if statement ALWAYS returned true. So I thought that maybe I was setting isAI to true somewhere but in the end I realized that I declared isAI as an optional type as shown below.

var isAI: Bool!

应该是

var isAI: Bool

这导致if语句没有检查如果isAI是真的,而是检查它是否包含一个值。

This resulted in the if-statement not checking if isAI was true, but instead checking if it contained a value.

所以为了安全起见我确保写下我的if-statments就像这样

So to be safe now I make sure to write my if-statments like this

if (hero.isAI == true) { //isAI is a Bool

}

所以我的问题是,将来有什么选择可以避免这个问题?(这个问题)看起来非常危险,特别是在大型项目团队工作时)。
我应该总是明确写出我的if-statment,我应该完全避免Bools的可选类型吗?

So my question is, what are my options to avoid this problem in the future? (this problem seems extremely dangerous, especially when working on a team on a large project).Should I always write my if-statment explicitly, should I just avoid the optional type for Bools altogether?

注意我在Xcode中没有这个问题Beta 2.当我升级到Xcode beta 3时出现了这个问题。我认为因为在Beta 2中,Apple通过检查其值而不是检查它是否包含值来在if语句中处理隐式解包的Bool。

Note that I did not have this problem in Xcode Beta 2. This problem came about when I upgraded to Xcode beta 3. I think because in Beta 2 Apple handled implicitly unwrapped Bool in an if-statement by checking its value rather than checking if it contains a value.

最后,下面是一个if-statements运行的例子,给出了一个可选的Bool来更好地帮助人们理解这个问题。

Lastly, below is an example of which if-statements run given an optional Bool to better help people understand the problem.

let myBool: Bool! = false

if (myBool) {
    //Runs
}

if (myBool!) {
    //Won't Run
}

if (!myBool) {
    //Runs
}

if (myBool == true) {
    //Won't Run
}


推荐答案

这是一个已知的问题,正在跟踪 repo,其中包含来自。

This is a known issue that is being tracked on the SwiftInFlux repo, which includes this quote from Chris Lattner on the Apple developer forums.

因此,这个问题不仅会影响可选的Bools,还会影响符合LogicValue协议(定义为)的任何可选类型。

So, this issue doesn't just effect optional Bools, but any optional type that conforms to the LogicValue protocol (defined as).

protocol LogicValue {
    func getLogicValue() -> Bool
}






无论如何关于如何解决这个问题的建议,很难推荐任何一个特定的解决方案,考虑到Apple未来没有说明他们打算如何解决这个问题,但我想会继续明确检查其价值。 Bool将是你要走的路。


Anyway as far as recommendations on how to work around this go, it's hard to recommend any one specific solution considering that Apple hasn't given an indication of how they intend to solve this in the future, but I would imagine that continuing to explicitly check the value of the Bool would be the way to go.

if (hero.isAI == true) {
    // stuff
}

事实上,在进一步阅读之后,上面列出的报价继续为: / p>

In fact, after some further reading, the quote listed above continues to read:

这篇关于Swift良好的编码实践:如果语句带有可选类型Bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:32