Swift 'NSArray?' does not have a member named 'count'

I'm pretty sure it .count

returns the length of the array, but for some reason when I used it, it threw a weird error.

Here is my code:

let dataFile = NSBundle.mainBundle().pathForResource(RopeDataFile, ofType: nil)
let ropes = NSArray(contentsOfFile: dataFile!);

for i in 0..<ropes.count {
}

      

RopeDataFile

is the property list constant I made earlier.

For some reason, it gives this error on ropes.count

,

'NSArray? does not have a member named 'count'.

I'm new to quick and pathetic if the problem is really simple.

+3


source to share


2 answers


NSArray(contentsOfFile: dataFile!)

returns an array optional . So you have to use like

ropes?.count

      



Or you can unpack the array first ropes

if let array = ropes{
       for i in 0..< array.count {
       }

    }

      

+6


source


ropes!.count 

      

use !



NSArray(contentsOfFile: dataFile!) return an optional array

      

+1


source







All Articles