Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some users can't get firstname and lastname #133

Open
mkaya93 opened this issue Aug 5, 2016 · 1 comment
Open

Some users can't get firstname and lastname #133

mkaya93 opened this issue Aug 5, 2016 · 1 comment

Comments

@mkaya93
Copy link

mkaya93 commented Aug 5, 2016

Hi

My problem is I can't get first and last name some of users with apaddressbook. also I'm looking my contacts that rows have lastname and firstname

let addressBook = APAddressBook()
addressBook.sortDescriptors = [NSSortDescriptor(key: "name.firstName", ascending: true)]
addressBook.fieldsMask = [APContactField.PhonesOnly, APContactField.Name]
addressBook.loadContacts { (contacts, error) in
            if error == nil {
                self.contacts = contacts!
            }else {
                print(error)
            }
        }
let person = contacts[indexPath.row]

                var firstName = person.name?.firstName
                var lastName = person.name?.lastName

                if firstName == nil || lastName == nil {
                    if firstName == nil {
                        firstName = ""
                    }
                    if lastName == nil {
                        lastName = ""
                    }
                    if firstName == nil && lastName == nil {
                        if person.phones![0].number != nil {
                            firstName = person.phones![0].number
                            lastName = ""
                        }else {
                            firstName = ""
                            lastName = ""
                        }
                    }
                }
                let fullName: String = firstName! + " " + lastName!
@belkevich
Copy link
Contributor

Hi,

The second part of the code looks very strange. You check is firstName/lastName nil, then assign empty string to them (so they not nil anymore!), and then check is firstName/lastName nil again. So, the following code will never executed:

if firstName == nil && lastName == nil {
    if person.phones![0].number != nil {
        firstName = person.phones![0].number
        lastName = ""
    } else {
        firstName = ""
        lastName = ""
    }
}

Try Example.app in this repo. If everything work fine I believe the problem is in your code. And if not I would ask you to help me to find the bug. But try Example.app first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants