【问题标题】:Swift: Set Address Property of ABRecordSwift:设置 ABRecord 的地址属性
【发布时间】:2015-11-03 03:28:30
【问题描述】:

我查看了 SE 问题 here [Avinash 的答案] 和 Apple 资源 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)

【问题讨论】:

  • 您会发现在 NSDictionary 世界而不是 CFDictionary 世界中工作要容易得多。除了 NSDictionary 之外,这里绝对不需要任何东西。只需用普通的 Swift 编写字典即可。
  • @matt 嗨,谢谢你的提示。我尝试了let dict: NSDictionary = [kABPersonAddressStreetKey : s["kABPersonAddressStreetKey"]!! as NSString,...,但得到一个错误,指出接受以下参数的下标没有重载。您或其他人能否提供一些示例代码来说明如何实施您的建议?
  • 什么是s?看起来它已经是你想要的字典了。
  • @matt 这是一个 [String : String?] 类型的常规(不是 NS)字典,除了地址之外,它还包含更多信息。另外,键是字符串类型,而不是 ABProperty,所​​以我不确定它是否可以在未经修改的情况下工作
  • 好的,但让我直说吧。您无法根据s 构建字典。您希望有人帮助您解决这个问题,以便您的代码能够编译。但是你不愿意展示s到底是什么?

标签: ios swift abaddressbook abrecord abmultivalue


【解决方案1】:

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

 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多