genaks
genaks

Reputation: 757

How to add elements to an array of custom objects in Swift

I am using this code to add custom objects to an array and then display that data in a custom TableView.

var tempCount = self.people?.count
for var k = 0 ; k < tempCount ; k++
{
    if let PERSON = self.people?[k]
    {
        let name = (PERSON.compositeName != nil) ? PERSON.compositeName  : ""
        let number = (PERSON.phoneNumbers?.first?.value != nil) ? PERSON.phoneNumbers?.first?.value : ""
        let image = (PERSON.image != nil) ? PERSON.image : UIImage(named: "aks.jpg")
        let details = Contact(userImage: image!, userName: name!, phoneNumber: number!)

        println(details.userName + "  " + details.phoneNumber)
        self.arrayOfContacts?.append(details)
        println(self.arrayOfContacts?.count)
    }
}

The count of the elements in the array always seems to be 'nil' for some reason. I have declared the array in the following manner

    var arrayOfContacts:[Contact]?

, Contact being the type of Object that array is supposed to contain.

and the other one as

    var people : [SwiftAddressBookPerson]? = []

The print statement does give out results but the object never gets added into the array.

Any idea about what I am doing wrong would be greatly helpful.

Upvotes: 1

Views: 3343

Answers (3)

Mario Rossi
Mario Rossi

Reputation: 1

Often when you’re dealing with data you don’t just have a fixed amount of elements. Take for example a program where you compute the average of multiple grades in a class:

var grade1 = 4
var grade2 = 3

var average = Double(grade1 + grade2) / 2.0
println("Average grade: \(average)")

What if we wanted the program to also work when we have 3 grades? We’d have to change our program to work with 3 grades.

var grade1 = 4
var grade2 = 3
var grade3 = 5

var average = Double(grade1 + grade2 + grade3) / 3.0
println("Average grade: \(average\)")

Upvotes: -1

Gwendal Rou&#233;
Gwendal Rou&#233;

Reputation: 4044

Your arrayOfContacts is nil, so arrayOfContacts?.count is nil as well.

If you really want to append to arrayOfContacts, don't write self.arrayOfContacts?.append(details) because this means "append to arrayOfContacts but actually I don't really care and if arrayOfContacts is nil, just give up".

Instead, write self.arrayOfContacts!.append(details), because now this means "append to arrayOfContacts, and since I really do care, tell me hard and loud, with a fatal crashing error, when arrayOfContacts is nil because well I'll have to figure out why on hell this array is nil when it should not be. I mean, I'm the master of the machine, not the opposite, and I know quite well that arrayOfContacts ought to be not nil when I want to append to it."

Upvotes: 1

Eric Aya
Eric Aya

Reputation: 70098

Your array is declared as an Optional array but is not created so it's nil.

Your declaration:

var arrayOfContacts:[Contact]?

Add the creation of an actual empty array:

arrayOfContacts = []

Or create it at once altogether:

var arrayOfContacts:[Contact]? = []

Upvotes: 5

Related Questions