0
votes

I have to create a 2D Array of Optional Bool type and compare the value inside it but I can't. 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.

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:

Binary operator '==' cannot be applied to operands of type 'Bool?.Type' (aka 'optional.Type') and '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:

cannot assign value of type 'Bool' to type 'Bool?.Type' (Aka'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.

3
variable names should be lower camelcase to prevent confusion with typesPranav Kasetti
Following the Apple doc, you should compare two booleans with == developer.apple.com/documentation/swift/bool/2430796. Maybe, the value you're getting with [Page][2] is not a boolDavid Luque

3 Answers

1
votes

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.

What you want is a (nested) array of Bool? values, initialized to 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)
0
votes

You should provide a fallback value with ??.

By the way, you don't need to write == false or == true, it's redundant.

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

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. 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.

Also please make sure you conform to the Swift naming convention, which is lower-camelCase for variable names.

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
        }
    }
}