Skip to main content

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.

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.

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.

added 172 characters in body
Source Link
genaks
  • 757
  • 2
  • 10
  • 24

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.

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.

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

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.

Source Link
genaks
  • 757
  • 2
  • 10
  • 24

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.

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