1

I am working in a TableViewController that I am trying to fill up with data from a plist.

I declared this at the top:

var studentsArray:Array<StudentData>?

And now I am doing the following in a function that loads my plist:

    var path = NSBundle.mainBundle().URLForResource("students", withExtension: "plist")
    let tmpArray = NSArray(contentsOfURL: path!)

    for studentDict in tmpArray!{
        let name = studentDict["name"]as! String
        let dateOfBirth = studentDict["dateOfBirth"] as! NSDate
        let regular = studentDict["regular"] as! Bool
        let photoFileName = studentDict["photoFileName"] as! String

        let data = StudentData(name : name, dateOfBirth: dateOfBirth, regular : regular, photoFileName: photoFileName)

        self.studentsArray?.append(data)
        println(studentsArray!.count)

    }

I tried logging the properties of the Object, they are all being filled in, but it goes wrong at the .append and somehow it just won't do that. The array's count remains 'Nil' when logged.

I'm kind of lost here! Any help is appreciated.

4
  • 2
    var studentsArray:[StudentData] = [] Commented May 15, 2015 at 2:26
  • 1
    @LeonardoSavioDabus Thank you so much, worked like a charm! Commented May 15, 2015 at 2:30
  • @LeonardoSavioDabus, you should post your comment as an answer. You were the first one to provide a correct solution. Commented May 15, 2015 at 2:44
  • @DuncanC Will M. answered the question. Commented May 15, 2015 at 2:50

2 Answers 2

4

Doesn't look like you are initializing studentsArray anywhere. Make sure to do that. For the record, array.count should be 0 if the array is empty, not nil. If you are seeing nil that means the array is probably nil (aka you didnt initialize it).

Sign up to request clarification or add additional context in comments.

Comments

0

Try changing your array declaration to this and let me know if it works. Hope it helps:

var studentsArray:Array<StudentData>? = Array<StudentData>()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.