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.
==
developer.apple.com/documentation/swift/bool/2430796. Maybe, the value you're getting with[Page][2]
is not a bool – David Luque