John Z
John Z

Reputation: 125

Swift 3 Contacts

I am using Swift Contacts and trying to determine if there is a way to cast contacts.phoneNumbers as a NSDictionary? i.e. values can be accessed in contacts.phoneNumbers this way:

self.officePhone.text = (contact.phoneNumbers[0].value ).value(forKey: "digits")as! String

Where the count is determined by how many phone numbers are entered for each contact by the user, however if I print the contact.phoneNumbers, I get a string like so:

label=_$!<Main>!$_, value=<CNPhoneNumber: 0x1452c870: countryCode=us, digits=7323214682>>

Where as it appears it is a key value pair, however if I try to cast to a NSDictionary for easy access I get Error:

Cannot convert value of type 'CNLabeledValue to expected argument types [AnyHashable : Any]

Any thoughts?

Upvotes: 0

Views: 440

Answers (1)

Josh Homann
Josh Homann

Reputation: 16327

This works for me:

    extension ViewController: CNContactPickerDelegate {
        func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
            let phoneNumbers = contact.phoneNumbers
            phoneNumbers.forEach {
                if let digits = $0.value.value(forKey: "digits") {
                    print(digits)
                }
            }
        }
    }

Upvotes: 1

Related Questions