从Swift类调用Objective

系统教程 行业动态 更新时间:2024-06-14 16:58:30
从Swift类调用Objective-C导致链接器错误(Calling Objective-C from Swift class causing linker errors)

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

创建头文件。 向它添加了头文件。 Swift可以看到这些类没有问题。 项目编译好。

在我尝试调用任何Objective类之后,出现构建期间的问题

例如:

override func viewDidLoad() { super.viewDidLoad() let rfDuinoManager: RFduinoManager = RFduinoManager.sharedRFduinoManager() }

体系结构x86_64的未定义符号:“_OBJC_CLASS _ $ _ RFduinoManager”,引用自:ViewController.o中的__TMaCSo14RFduinoManager o未找到体系结构x86_64的符号:clang:error:linker命令失败,退出代码1(使用-v查看调用)

林不知道我在这里失踪。 链接到这里的项目。 https://www.dropbox.com/s/rza1ce01g4q5lp6/SmartHomeHub-stackoverflow.zip?dl=0

预先感谢您的帮助。 现在诚实地放弃希望,并考虑将整个库改写为Swift

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

最满意答案

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

删除rfduino文件夹(在左侧的项目导航器面板中选择rfduino文件夹并点击删除按钮,但当它要求将其移至垃圾箱或仅删除引用时,请选择“删除引用”。

在这里输入图像描述

您的桥接头有一个绝对路径引用,我建议通过在构建设置中选择并删除按钮来完全删除它:

在这里输入图像描述

将rfduino文件重新添加回项目,这次在“添加文件夹”选项下,选择“创建组”,而不是“创建文件夹引用”。 如果您希望它提示为您创建适当的桥接标题,请不要选择文件夹,而是选择单个文件:

在这里输入图像描述

还要确保SmartHomeHub在下面进行选中。

如果你这样做,它会要求你自动创建桥接头:

在这里输入图像描述

转到这个新的桥接头并再次添加导入行:

在这里输入图像描述

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

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".

enter image description here

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

enter image description here

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:

enter image description here

Also make sure that SmartHomeHub is checked below.

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

enter image description here

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

enter image description here

更多推荐

本文发布于:2023-04-15 03:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4805b689baaba147c30d1b7b1b63c03f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   Objective

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!