Swift (iOS 8 SDK) Convert Unmanaged to ABMultiValueRef

前端 未结 3 936
执念已碎
执念已碎 2020-12-24 15:14

I need to convert the return value of this function from the AddressBook framework:

ABRecordCopyValue(nil, kABPersonPhoneProperty)

to a val

3条回答
  •  不思量自难忘°
    2020-12-24 15:37

    In case someone is looking for a final way to deal with ABRecords in Swift 2, here it is:

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord) {
    
        let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue()
        let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue()
    
        var emails:[String] = []
        let emailRecords = ABRecordCopyValue(person, kABPersonEmailProperty).takeRetainedValue() as ABMultiValueRef
        let emailsCount = ABMultiValueGetCount(emailRecords)
        for index in 0 ..< emailsCount {
            if let email = ABMultiValueCopyValueAtIndex(emailRecords, index).takeRetainedValue() as? String {
                emails.append(email)
            }
        }
    
        print("Contact selected. firstName: \(firstName), lastName: \(lastName), emails: \(emails)")
    
    }
    

提交回复
热议问题