Swift良好的编码实践:如果语句带有可选类型Bool [英] Swift good coding practice: If statement with optional type Bool

查看:118
本文介绍了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
}


推荐答案

这是一个已知的问题,正在跟踪 SwiftInFlux repo,其中包含来自 Chris Lattner的报价Apple开发者论坛

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.


任何符合
LogicValue协议的可选项都存在此问题(例如嵌套的选项,可选的bool,
等)。我们认为需要修复1.0和
的严重问题有一些想法,但尚未确定解决方案。

This problem exists with any optional of something that conforms to the LogicValue protocol (e.g. nested optionals, optional of bool, etc). We consider it serious issue that needs to be fixed for 1.0 and have some ideas, but haven't settled on a solution yet.

因此,这个问题不仅会影响可选的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:


对于这种常见情况,最简单的答案是为if x产生
警告,并要求某人明确地写if x!=
nil或if x == true使其明确表达他们想要的内容。

For this common case, the simplest answer would be to produce a warning for "if x" and require someone to explictly write "if x != nil" or "if x == true" to make it explicit what they want.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆