Arrays – Quantity of the NIL value in the Arrary of Int Optionals

I just started using Swift and using options. After using generateRandomArrayOfIntsAndNils(), I struggled to calculate the number of nils in the test array

This is what I want to use Method:

let array1: [Int?]
array1 = generateRandomArrayOfIntsAndNils()\

var total = 0

for i in array1 {
if(array1[i] == nil){
total += 1
}
}

print(total)

Any suggestions or guidance would be greatly appreciated.

you You can do this:

let countNils = array1.filter({ $0 == nil }).count

< p>I just started using Swift and using options. After using generateRandomArrayOfIntsAndNils(), I struggled to calculate the number of nils in the test array

This is the method I want to use:

let array1: [Int?]
array1 = generateRandomArrayOfIntsAndNils()\

var total = 0

for i in array1 {
if(array1[i] == nil){
total += 1
}
}

print(total)

< p>Any suggestions or guidance would be greatly appreciated.

You can do this:

 let countNils = array1.filter({ $0 == nil }).cou nt

Leave a Comment

Your email address will not be published.