【问题标题】:Cannot find protocol declaration for 'ABPeoplePickerNavigationControllerDelegate'找不到“ABPeoplePickerNavigationControllerDelegate”的协议声明
【发布时间】:2015-05-28 23:34:04
【问题描述】:

在一个已经有 ObjC 的项目中,我正在添加一个 Swift 类

import AddressBookUI
class MyVC : UITableViewController, ABPeoplePickerNavigationControllerDelegate {
}

MyApp-Swift.h:289:42:找不到“ABPeoplePickerNavigationControllerDelegate”的协议声明;你的意思是“UINavigationControllerDelegate”吗?

不,斯威夫特,我的意思是ABPeoplePickerNavigationControllerDelegate。真的想知道我在这里做错了什么......

【问题讨论】:

  • 你认为我没有?自 2008 年以来我一直在使用 AB 框架......在 Swift 中有点不同——它可能与 AB 无关,但通常我不理解 Swift/ObjC 的某些内容。
  • 只需导入一个快速文件名 MyVC 并使用上面的代码,不要将上面的代码放在头文件中。

标签: ios swift delegates addressbook peoplepicker


【解决方案1】:

我需要添加

#import <AddressBookUI/AddressBookUI.h>

在我的Briding-Header.h。可以认为我的 Swift 文件中的 import 就足够了。不是。

也就是说,现在我在实施时遇到了一个新问题

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
}

出现下一个错误:

-Swift.h:297:110:需要一个类型

ABRecord 输入有问题

didSelectPerson:(ABRecord)

如果我还在 Briding Header 或 Swift 文件中导入 AddressBook 也无济于事。

【讨论】:

  • 你为什么要在你的头文件中添加所有这些东西
【解决方案2】:

您可以查看我使用的代码here

纯 Swift 项目,不涉及 Objective-C

对我来说,这是很好的编译不使用任何桥接头

import UIKit
import AddressBook
import AddressBookUI

class ViewController: UITableViewController, ABPeoplePickerNavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {


    }
}

我正在将相关框架(AddressBook、AddressBookUI)添加到目标的 link binary with libraries 阶段

Objective-C 项目,带有桥接头

我的 Bridging-Header.h:

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
  • 确保您的 Bridging-Header 在您的目标中正确引用

  • swift VC 代码同上

【讨论】:

  • 你的项目中有 ObjC 还是直接 Swift?
  • 这只是我为测试您的问题而创建的一个纯 Swift 示例项目。您在 Objective-C 项目中遇到过这个问题吗?
【解决方案3】:

感谢@neonacho on Twitter,解决了我的第二个问题。而不是ABRecord 我不得不使用ABRecordRef 来编译。不知道为什么 Diego 的代码有效而不是我的。于是就变成了

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, 
                                      didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
}

它有效。

【讨论】:

    【解决方案4】:

    FWIW,这在纯 Swift 中与 ABRecord 一起使用,但在 Objective-C 兼容性标头中不起作用的原因是有一个 typealias 后者显然不能正确转换回来:

    typealias ABRecordRef = ABRecord

    https://developer.apple.com/library/prerelease/ios/documentation/AddressBook/Reference/ABRecordRef_iPhoneOS/index.html#//apple_ref/c/tdef/ABRecordRef

    可能值得提交雷达?

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      相关资源
      最近更新 更多