简体   繁体   English

比较可选的布尔变量不起作用

[英]Compare Optional Bool variable doesn't work

I have to create a 2D Array of Optional Bool type and compare the value inside it but I can't. 我必须创建一个可选Bool类型的2D Array ,并比较其中的值,但我不能。 The first time I try to declare in this way: 我第一次尝试以这种方式声明:

var Answ = [[Bool?]] ()
var Page = 0 

for i in 0...4
{
    if Answ[Page][i] ==  true
    {...}

    else if Answ[Page][I] == false
    {...}

    else
    {...}

}
...

but when I launch the program, it says: 但是当我启动程序时,它说:

index out of range 索引超出范围

when Xcode compares the Answ[Page][i] with the value true . 当Xcode将Answ[Page][i]与值true

So, I try to modify the code and declare the array in this way: 因此,我尝试以这种方式修改代码并声明数组:

var Answ = Array (repeating: Array (repeating: Bool?.self , count: 5), count: 40)
var Page = 0 

for i in 0...4
{
    if Answ[Page][i] ==  true
    {...}

    else if Answ[Page][I] == false
    {...}

    else
    {...}

}
...

but at the same point, (if Answ[Page][i] == true) throws this error: 但同时, (if Answ[Page][i] == true)抛出此错误:

Binary operator '==' cannot be applied to operands of type 'Bool?.Type' (aka 'optional.Type') and 'Bool'" 二进制运算符'=='不能应用于类型为'Bool?.Type'(又名'optional.Type')和'Bool'“的操作数

Moreover, in other points of the code where I try to set a value of the array as true/false ( Answ[Page][2] = true ), Xcode says this: 此外,在代码的其他地方,我试图将数组的值设置为true / false( Answ[Page][2] = true ),Xcode表示:

cannot assign value of type 'Bool' to type 'Bool?.Type' (Aka'Optional.Type') 无法将类型'Bool'的值分配为类型'Bool?.Type'(又名'Optional.Type')

Can someone help me, please? 有人能帮助我吗? Thank you in advance. 先感谢您。

I found this topic: 我发现了这个话题:

Checking the value of an Optional Bool 检查可选布尔值

but it didn't help me much. 但这并没有太大帮助。

You can compare optional bools as in the Q&A that you linked to. 可以像链接到“问答”中那样比较可选的布尔值。 The problem is that 问题是

var Answ = Array (repeating: Array (repeating: Bool?.self , count: 5), count: 40)

creates a (nested) array of Bool?.self , which is the type of an optional bool, not a value of that type. 创建Bool?.self的(嵌套)数组,该数组是可选的bool的类型 ,而不是该类型

What you want is a (nested) array of Bool? 您想要的是(嵌套的) Bool?数组Bool? values, initialized to nil : 值,初始化为nil

var answers: [[Bool?]] = Array(repeating: Array(repeating: nil , count: 5), count: 40)

or alternatively: 或者:

var answers = Array(repeating: Array(repeating: Bool?.none , count: 5), count: 40)

You should provide a fallback value with ?? 您应提供带有??后备值 .

By the way, you don't need to write == false or == true , it's redundant. 顺便说一句,您无需编写== false== true ,这是多余的。

if Answ[Page][i] ?? false {
    [...]
}

There are several issues with your code. 您的代码有几个问题。 First of all, don't use manual indexing in a for loop, rather use for ... in to have the indexes automatically handled for you. 首先,请勿在for循环中使用手动索引,而应使用for ... in来为您自动处理索引。 Secondly, a better solution for handling optional booleans is to safely unwrap the value using optional binding and then check the non-optional value. 其次,处理可选布尔值的更好解决方案是使用可选绑定安全地解开该值,然后检查非可选值。 You also don't need to write if bool == true , if bool has the same meaning. if bool == trueif bool具有相同的含义, if bool也不需要编写。

Also please make sure you conform to the Swift naming convention, which is lower-camelCase for variable names. 另外,请确保您遵循Swift命名约定,对于变量名,该约定为lower-camelCase。

var answ = [[Bool?]] ()
var page = 0

for ans in answ[page]{
    if let unwrappedAns = ans {
        if unwrappedAns {
            // answ[page][i] = true
        } else {

        }
    } else {
        //answ[page][i] = ans is nil
    }
}

If you actually want to iterate through the whole array of arrays, this is one safe way for doing so: 如果您实际上想要遍历整个数组,则这是一种安全的方法:

for page in answ {
    for ans in page {
        //here ans = Answ[Page][i] when compared to the code in your question
        if let unwrappedAns = ans {
            if unwrappedAns {

            } else {

            }
        } else {
            //ans is nil
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM