1

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.

1
  • I don't see where you initialize your array. at some point you should have self.people = [] Commented Aug 31, 2015 at 11:53

3 Answers 3

5

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]? = []
Sign up to request clarification or add additional context in comments.

4 Comments

How in the world could I be so dumb! Thanks!
I bet you're not dumb at all. Mistakes just happens sometimes. :)
I'd bet he's new to swift which makes him a NOOB! C:
what @A'saDickens said!
1

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."

4 Comments

Please: don't. The OP is right to use optional binding, that way it doesn't crash if the array is nil, whereas with forced unwrap as in your example it will crash on the user side, which is never good. If you know your array will never be nil, then don't make it an Optional at all. Swift's compiler helps you manage bugs at compile time, don't defer them at runtime.
Indeed. A crash is exactly what I would expect in this case. This array must not be nil at the time I append to it, and I translate this "must" into the code with the forced unwrapped optional. Now I fix the code around in order to make sure this never crashes.
If a non-optional property is the solution, so be it. But sometimes it is not, and when the compiler can't help me, I actively help me myself, by making sure my code crashes loud and clear at the very moment my expectations start to fail.
But after falling flat on my face an umpteen number of time, I have finally learned to always safely unwrap a variable rather than forcing it to unwrap so the app atleast never crashes.. The crash is the last thing a user would want to see in an app.. So it's better it crashes in my face when I develop it rather than it does when the users use it
-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\)")

3 Comments

You must have made a missclick somewhere, your answer is not related to the question.
That came out of nowhere :D
idk i see the relevance, because what if we have 4 grades? Then, yet again, we'd have to change our program to work with 4 grades. var grade1 = 4 var grade2 = 3 var grade3 = 3 var grade4 = 3 then you would have to put var average = Double(grade1 + grade2 + grade3 + grade4)/ 4.0 printLn("Average grade: (average)"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.