gpt4 book ai didi

ios - Swift:设置 ABRecord 的地址属性

转载 作者:行者123 更新时间:2023-11-28 07:01:24 24 4
gpt4 key购买 nike

我看了一下SE问题here [Avinash 的回答] 和 Apple 资源 here [第 18 页底部和第 19 页顶部] 我试图为 ABRecord 设置地址。我尽力从 Objective-C 翻译它们,但显然我在某处犯了一个错误,因为在行 let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) 我得到了错误 无法分配给“CFDictionaryValueCallBacks”类型的不可变值

这是我的代码:

    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)

最佳答案

我确信有更简洁的方法可以做到这一点,但这是我想出的解决方案。

 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)

关于ios - Swift:设置 ABRecord 的地址属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com