Swift: Set Address Property of ABRecord

444 views Asked by At

I took a look at the SE question here [Avinash's answer] and the Apple resource here [bottom of page 18 and top of page 19] in my attempt to set an address for an ABRecord. I did my best to translate them from Objective-C, but apparently I made an mistake somewhere since on the line let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) I get the error Cannot assign to immutable value of type 'CFDictionaryValueCallBacks'.

Here's my code:

    let information: ABRecord = ABPersonCreate().takeRetainedValue()

    let address: ABMutableMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABMultiDictionaryPropertyType)).takeUnretainedValue()

    var keys = [CFStringRef]()
    var values = [CFStringRef]()

    keys.append(kABPersonAddressStreetKey)
    keys.append(kABPersonAddressCityKey)
    keys.append(kABPersonAddressStateKey)
    keys.append(kABPersonAddressZIPKey)
    keys.append(kABPersonAddressCountryKey)
    keys.append(kABPersonAddressCountryCodeKey)

    Note: country code left out

    values.append(s["kABPersonAddressStreetKey"]!! as NSString)
    values.append(s["kABPersonAddressCityKey"]!! as NSString)
    values.append(s["kABPersonAddressStateKey"]!! as NSString)
    values.append(s["kABPersonAddressZIPKey"]!! as NSString)
    values.append(s["kABPersonAddressCountryKey"]!! as NSString)
    values.append(s["kABPersonAddressCountryCodeKey"]!! as NSString)

    let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)

    let scanned = ABUnknownPersonViewController()

    let identifier = ABMultiValueIdentifier()

    ABMultiValueAddValueAndLabel(address, dict, kABHomeLabel, &identifier)

    ABRecordSetValue(information, kABPersonAddressProperty, address, nil)
1

There are 1 answers

0
Randoms On BEST ANSWER

I'm sure there's a much more concise way to do this, but here's the solution I've come up with.

 var addressComponents = [String : String]()

    if let value = s["kABPersonAddressStreetKey"] {
        addressComponents[kABPersonAddressStreetKey as String] =  value
    }

    if let value = s["kABPersonAddressCityKey"] {
        addressComponents[kABPersonAddressCityKey as String] = value
    }

    if let value = s["kABPersonAddressStateKey"] {
        addressComponents[kABPersonAddressStateKey as String] = value
    }

    if let value = s["kABPersonAddressZIPKey"] {
        addressComponents[kABPersonAddressZIPKey as String] = value
    }

    if let value = s["kABPersonAddressCountryKey"] {
        addressComponents[kABPersonAddressCountryKey as String] = value
    }

    if let value = s["kABPersonAddressCountryCodeKey"] {
        addressComponents[kABPersonAddressCountryCodeKey as String] = value
    }

    let address: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
    ABMultiValueAddValueAndLabel(address, addressComponents, kABHomeLabel, nil)
    ABRecordSetValue(information, kABPersonAddressProperty, address, nil)