从Swift类调用Objective-C导致链接器错误 [英] Calling Objective-C from Swift class causing linker errors

查看:126
本文介绍了从Swift类调用Objective-C导致链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Swift中使用RFDuino Objective-C库.听起来很简单,我之前从Swift类中调用过Objective-C,没问题.但这一次我撞到了砖墙.

创建的头文件.添加了头文件. Swift可以看到类没有问题.项目编译正常.

在我尝试调用任何Objective类后,构建过程中就会出现问题

例如:

    override func viewDidLoad() {
         super.viewDidLoad()

        let rfDuinoManager: RFduinoManager = RFduinoManager.sharedRFduinoManager()

}

体系结构x86_64的未定义符号: 从以下位置引用的"_OBJC_CLASS _ $ _ RFduinoManager" __TMaCSo14RFduinoManager在ViewController.o中 ld:找不到架构x86_64的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

我不确定我在这里缺少什么. 链接到这里的项目. https://www.dropbox.com/s/rza1ce01g4q5lp6/SmartHomeHub -stackoverflow.zip?dl=0

预先感谢您的帮助.老实说现在失去希望,并考虑将整个库重写为Swift

解决方案

问题是如何将rfduino文件夹添加到项目中.所以

  1. 删除rfduino文件夹(在左侧的项目导航器面板中选择rfduino文件夹,然后单击Delete按钮,但是当它要求将其移至垃圾箱或只是删除引用时,请选择删除引用". >

  2. 您的桥接头具有绝对路径引用,我建议通过在构建设置中选择它并单击删除按钮将其完全删除:

  3. 这一次将rfduino文件重新添加到项目中,这一次,在添加的文件夹"选项下,选择创建组"而不是创建文件夹引用".如果您希望它提示您为您创建适当的桥接头,请不要选择文件夹,而要选择单个文件:

    还要确保在下面选中了SmartHomeHub.

  4. 如果操作正确,它将要求您自动创建桥接头:

  5. 转到此新的桥接标头,然后再次添加导入行:

I'm trying to use RFDuino Objective-C library in Swift. It all sounds simple, I have called Objective-C from Swift classes before, no problem. This time however I hit the brick wall.

Created header file. Added header files to it. Swift can see the classes no problem. Project compiles fine.

The problem during build appears just after I try to call any of Objective classes

for example:

    override func viewDidLoad() {
         super.viewDidLoad()

        let rfDuinoManager: RFduinoManager = RFduinoManager.sharedRFduinoManager()

}

Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_RFduinoManager", referenced from: __TMaCSo14RFduinoManager in ViewController.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Im not sure what Im missing here. Link to the project here. https://www.dropbox.com/s/rza1ce01g4q5lp6/SmartHomeHub-stackoverflow.zip?dl=0

Thanks in advance for help. Honestly loosing hope now and considering rewrite whole library to Swift

解决方案

The problem is how the rfduino folder was added to the project. So

  1. Remove rfduino folder (selecting rfduino folder in the project navigator panel on left and hit delete button, but when it asks to move it to the trash or just remove references, choose "remove references".

  2. Your bridging header has an absolute path reference, I'd suggest removing that altogether by selecting it in build settings and hitting delete button:

  3. Re-add rfduino files back to the project, this time, under "added folders" option, choose "create groups" rather than "create folder references". If you want it to prompt to create proper bridging header for you, don't select folder, but select the individual files:

    Also make sure that SmartHomeHub is checked below.

  4. If you do that properly it will ask you to create bridging header automatically:

  5. Go to this new bridging header and add your import lines again:

这篇关于从Swift类调用Objective-C导致链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆