当前位置: 首页 > 知识库问答 >
问题:

检查可选布尔值

楚勇
2023-03-14

当我想检查可选布尔值是否为真时,这样做行不通:

var boolean : Bool? = false
if boolean{
}

它会导致以下错误:

可选类型“@IvalueBool?”不能用作布尔值;测试'!=而不是零

我不想检查nil;我想检查返回的值是否为真。

如果布尔值==true,我是否总是必须执行如果我使用可选布尔值?

既然Optionals不再符合BooleanType,编译器难道不应该知道我想检查Bool的值吗?


共有3个答案

龙华翰
2023-03-14
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")
}
万俟穆冉
2023-03-14
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)'")
}
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)'")
}

如果booleanValuenil并且if块不执行,则代码let booleanValue=booleanValue返回false。如果booleanValue不是nil,则此代码定义了一个名为booleanValue的新变量Bool类型(而不是可选的Bool?)。

斯威夫特3

注意:将绑定的常量/变量命名为与可选常量/变量相同的Swift约定,例如let booleanValue=booleanValue。这种技术称为变量阴影。您可以打破常规并使用类似let unwrapedBooleanValue=booleanValue, unwrapedBooleanValue的东西。我指出这一点是为了帮助理解发生了什么。我建议使用变量阴影。

对于这种特殊情况,零合并是明确的

var booleanValue : Bool? = false
if booleanValue ?? false {
    // executes when booleanValue is true
    print("optional booleanValue: '\(booleanValue)'")
}

检查false不太清楚

var booleanValue : Bool? = false
if !(booleanValue ?? false) {
    // executes when booleanValue is false
    print("optional booleanValue: '\(booleanValue)'")
}

注意:if! booleanValue?? false不编译。

强制展开会增加将来有人进行更改的可能性,该更改会编译,但会在运行时崩溃。因此,我会避免这样的事情:

var booleanValue : Bool? = false
if booleanValue != nil && booleanValue! {
    // executes when booleanValue is true
    print("optional booleanValue: '\(booleanValue)'")
}

尽管这个堆栈溢出问题特别询问如何检查Bool?if语句中是否为true,但确定检查true、false或将未包装的值与其他表达式组合的一般方法很有帮助。

随着表达式变得越来越复杂,我发现可选绑定方法比其他方法更灵活、更容易理解。请注意,可选绑定适用于任何可选类型(Int?String?等)。

牛越
2023-03-14

使用可选布尔值,需要显式检查:

if boolean == true {
    ...
}

否则,您可以打开可选:

if boolean! {
    ...
}

但如果布尔值为nil,则会生成一个运行时异常,以防止出现这种情况:

if boolean != nil && boolean! {
    ...
}

在beta 5之前,这是可能的,但正如发行说明中所述,它已经发生了更改:

选项在有值时不再隐式评估为true,没有值时不再隐式评估为false,以避免在使用可选Bool值时出现混淆。相反,使用==或!=运算符显式检查nil以确定可选是否包含值。

附录:正如@MartinR所建议的,第三个选项的更紧凑的变体是使用聚结运算符:

if boolean ?? false {
    // this code runs only if boolean == true
}

这意味着:如果布尔值不是nil,则表达式的计算结果为布尔值(即使用未包装的布尔值),否则表达式的计算结果为false

 类似资料:
  • 除了相等检查,Jasmine还提供了一些方法来检查布尔条件。 以下是帮助我们检查布尔条件的方法。 toBeTruthy() 此布尔匹配器在Jasmine中用于检查结果是等于true还是false。 以下示例将帮助我们理解toBeTruthy()函数的工作原理。 ExpectSpec.js describe("Different Methods of Expect Block",function (

  • 问题内容: 我有一个mongo文档,其中包含一个日期字段,该日期字段也可以为false(或未定义),并且似乎无法找到如何检查该字段是否可用或为false或是否为日期(time.Time)的日期golang / mgo:S 问题答案: 如果您有一个字段,并且想知道它是否正确地设置了有效日期,则可以查询其方法。否则,如果您要在数据库中查询此类文档,则可以执行以下操作之一。 查询该字段是否为假: 使用$

  • 我在Java 8上工作,有一个简单的问题我还没有解决。 我有3个方法来验证来自数据库的数据,并根据它们是否得到一行来返回真或假。棘手的是,即使我知道第一部分返回false,我仍然希望它检查其他两个方法。我已经编写了这样的代码: 问题是,当validateMETOD1()返回false时,它没有调用validateMETOD2()。有人能解释一下为什么吗?我试过了: 仍然面临同样的问题。

  • 我试图获得以下行为: python test.py= 当我使用 但是,如果我添加,试图强制强制强制转换为布尔值,它就会中断。在这种情况下 实际上最终存储。怎么回事??

  • 问题内容: 如何检查布尔值是否为null?因此,如果我知道“ hideInNav”为空。如何阻止它进一步执行?像下面这样的东西似乎不起作用,但是为什么呢? 问题答案: 只能是或因为它是原始数据类型(+ 变量的默认值为)。如果要使用值,则可以改用类。布尔是一种引用类型,这就是您可以分配给布尔“变量”的原因。例:

  • 在java以下作品: