Swift: 设置ABRecord的地址属性
Swift: Set Address Property of ABRecord
我查看了 SE 问题 here [Avinash's answer] and the Apple resource here [第 18 页底部和第 19 页顶部],试图为 ABRecord
设置地址。我尽力从 Objective-C 翻译它们,但显然我在某处犯了一个错误,因为在行 let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
上我得到了错误 Cannot assign to immutable value of type '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)
我查看了 SE 问题 here [Avinash's answer] and the Apple resource here [第 18 页底部和第 19 页顶部],试图为 ABRecord
设置地址。我尽力从 Objective-C 翻译它们,但显然我在某处犯了一个错误,因为在行 let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
上我得到了错误 Cannot assign to immutable value of type '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)