Moo*_*Cat 76 boolean optional swift
当我想检查一个Optional Bool是否为true时,执行此操作不起作用:
var boolean : Bool? = false
if boolean{
}
Run Code Online (Sandbox Code Playgroud)
它导致此错误:
可选类型'@IvalueBool?' 不能用作布尔值; 测试'!= nil'而不是
我不想检查零; 我想检查返回的值是否为true.
if boolean == true如果我正在使用Optional Bool,我是否总是必须这样做?
由于Optionals不再符合BooleanType,编译器是否应该知道我想检查Bool的值?
Ant*_*nio 176
使用可选的布尔值,需要明确检查:
if boolean == true {
...
}
Run Code Online (Sandbox Code Playgroud)
否则你可以解开可选的:
if boolean! {
...
}
Run Code Online (Sandbox Code Playgroud)
但是,如果布尔值为nil- 则会产生运行时异常- 以防止:
if boolean != nil && boolean! {
...
}
Run Code Online (Sandbox Code Playgroud)
在测试版5之前,它是可能的,但它已根据发行说明中的报告进行了更改:
Optionals在有值时不再隐式评估为true,而在没有值时则不再为false,以避免在使用可选的Bool值时出现混淆.相反,使用==或!=运算符对nil进行显式检查,以确定可选项是否包含值.
附录:正如@MartinR所建议的,第三种选择的更紧凑变体是使用合并运算符:
if boolean ?? false {
// this code runs only if boolean == true
}
Run Code Online (Sandbox Code Playgroud)
这意味着:如果boolean不是nil,则表达式求值为布尔值(即使用展开的布尔值),否则表达式求值为 false
Mob*_*Dan 39
var booleanValue : Bool? = false
if let booleanValue = booleanValue, booleanValue {
// Executes when booleanValue is not nil and true
// A new constant "booleanValue: Bool" is defined and set
print("bound booleanValue: '\(booleanValue)'")
}
Run Code Online (Sandbox Code Playgroud)
var booleanValue : Bool? = false
if let booleanValue = booleanValue where booleanValue {
// Executes when booleanValue is not nil and true
// A new constant "booleanValue: Bool" is defined and set
print("bound booleanValue: '\(booleanValue)'")
}
Run Code Online (Sandbox Code Playgroud)
如果是,则代码let booleanValue = booleanValue返回false,并且块不执行.如果不是,则此代码定义一个名为type 的新变量(而不是可选的).booleanValuenilifbooleanValuenilbooleanValueBoolBool?
Swift 3和4代码booleanValue(以及Swift 2.2代码where booleanValue)评估新booleanValue: Bool变量.如果为true,则if块booleanValue: Bool在范围内使用新定义的变量执行(允许该选项在if块内再次引用绑定值).
注意:将绑定常量/变量命名为与可选常量/变量相同的是Swift约定,例如let booleanValue = booleanValue.这种技术称为变量阴影.你可以打破常规并使用类似的东西let unwrappedBooleanValue = booleanValue, unwrappedBooleanValue.我指出这一点是为了帮助理解正在发生的事情.我建议使用变量阴影.
对于这种特定情况,没有合并是明确的
var booleanValue : Bool? = false
if booleanValue ?? false {
// executes when booleanValue is true
print("optional booleanValue: '\(booleanValue)'")
}
Run Code Online (Sandbox Code Playgroud)
检查false不是很清楚
var booleanValue : Bool? = false
if !(booleanValue ?? false) {
// executes when booleanValue is false
print("optional booleanValue: '\(booleanValue)'")
}
Run Code Online (Sandbox Code Playgroud)
注意:if !booleanValue ?? false不编译.
强制解包会增加某人在未来编译但在运行时崩溃的可能性.因此,我会避免这样的事情:
var booleanValue : Bool? = false
if booleanValue != nil && booleanValue! {
// executes when booleanValue is true
print("optional booleanValue: '\(booleanValue)'")
}
Run Code Online (Sandbox Code Playgroud)
虽然这个堆栈溢出问题具体询问如何检查a Bool?是否true在if语句中,但是确定检查true,false或将unwrapped值与其他表达式组合的一般方法是有帮助的.
随着表达式变得更加复杂,我发现可选绑定方法比其他方法更灵活,更易于理解.注意与任何可选类型(即可选绑定工作Int?,String?等等).
Ado*_*els 11
var enabled: Bool? = true
if enabled == true {
print("when is defined and true at the same moment")
}
if enabled == false {
print("when is defined and false at the same moment")
}
if let enabled = enabled, enabled == true {
print("when is defined and true at the same moment")
}
if let enabled = enabled, enabled == false {
print("when is defined and false at the same moment")
}
if let enabled = enabled, enabled {
print("when is defined and true at the same moment")
}
if let enabled = enabled, !enabled {
print("when is defined and false at the same moment")
}
if enabled ?? false {
print("when is defined and true at the same moment")
}
if enabled == .some(true) {
print("when is defined and true at the same moment")
}
if enabled == (true) {
print("when is defined and true at the same moment")
}
if case .some(true) = enabled {
print("when is defined and true at the same moment")
}
if enabled == .some(false) {
print("when is defined and false at the same moment")
}
if enabled == (false) {
print("when is defined and false at the same moment")
}
if enabled == .none {
print("when is not defined")
}
if enabled == nil {
print("when is not defined")
}
Run Code Online (Sandbox Code Playgroud)