如何在Swift项目中使用超级lib [英] How to use Superpowered lib in Swift project

查看:597
本文介绍了如何在Swift项目中使用超级lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Swift中制作一个应用程序,只需通过iPhone的麦克风录音,然后播放录音。



为此,我想使用具有头文件的静态库lib Superpowered。



为此,我试图拖放.a和头文件在我的项目我也是一个桥接头文件),在Xcode> Target> General(因此在Xcode> Target> Build阶段>Link Binary With Libraries中的Linked Frameworks and Libraries中添加.a)在Xcode>目标>构建设置>搜索路径>库搜索路径。



但是,我有错误ld:符号未找到架构arm64
与此处完全相同



Bridging-Header-文件与我想使用的lib方法的原型在我的Swift代码(我不喜欢这个,但如果它是唯一的方式...)。





在Swift中的代码所在的viewController文件中,我可以创建一个超级对象谢谢Bridging-Header-File,并且调用我已经放入的方法。





我甚至不明白为什么会这样Objective-C ++文件在这里和它包含什么。



在这个示例项目中,我创建了自己的项目,下面是一些屏幕截图:

p>



同样的桥接头示例项目,除了我包括 SuperpoweredIOSAudioIO.h ,所以我可以使用 SuperpoweredIOSAudioIODelegate





我的viewController文件,Swift中的代码是,我可以创建一个超级对象谢谢Bridging-Header-File,并调用其中的方法。



直到这里,这是伟大的,除了我不能例如创建一个 SuperpoweredRecorder 对象。如果我尝试在我的Bridging-Header-File中包含 SuperpoweredRecorder.h 文件,我有以下错误:





所以我看到这是因为 SuperpoweredRecorder.h 包含一些.cpp文件,我必须创建一个包装的cpp(有点像我用桥接头,没有?),但包括一个.h和一个.mm文件,我不知道我必须放在那里。 mm文件( SuperpoweredRecorder.cpp 的代码,但我无法访问它)



,我有点困惑与所有的东西,你能帮助我了解如何使用所有的超级权力lib在我的Swift项目吗?

解决方案

正如我在评论@OmniProg时说的,我与Superpowered的CTO进行了一次对话,帮助我找到了下面的解决方案。



因此,由于Swift不能直接与C ++交互,但是可以使用Objective-C,我必须在Objective-C ++(.mm文件,C ++和Objective-C之间的混合)中创建对象,它包装lib的C ++类SuperPowered 。



下面是来自lib的SuperpoweredRecorder对象的示例:





在这里我创建一个.h文件,其中我使用名称 SuperpoweredRecorderWrapped 也在其原型中使用我想使用的lib的 SuperpoweredRecorder 的所有方法。





然后我创建一个新的.m文件,重命名.mm,然后实现 SuperpoweredRecorderWrapped 就可以了。
我导入 SuperpoweredRecorderWrapped.h SuperpoweredRecorder.h
我创建一个 SuperpoweredRecorder 对象作为名为 _wrapped 的属性,在我的方法中,我调用相应的方法对象。 _wrapped
这样,当我在Swift代码中调用 SuperpoweredRecorderWrapped 开始调用启动 _wrapped SuperpoweredRecorder 对象。看到诀窍?





最后,我将所有

:所有的C ++代码都应该是我的Bridging-Header中的in .mm文件,这就是为什么我让我的 #include of .h在.mm文件中而不是在.h文件中包含C ++代码。


I want to make an app in Swift that simply record via the mic of the iPhone and then play the sound recorded.

For that, I'd like to use the lib Superpowered that is a static library with header files.

For that, I tried to drag and drop the .a and headers files in my project (Xcode create for me a bridging header file), add the .a in "Linked Frameworks and Libraries" in Xcode > Target > General (and so in Xcode > Target > Build phases > "Link Binary With Libraries" too) and index the .h files path in Xcode > Target > Build Settings > Search Paths > Library Search Paths.

But with that I have the error "ld: symbol(s) not found for architecture arm64" Exactly the same as here XCode 5: Undefined symbols for architecture arm64 error and I tried all the solutions that I found on the web for that, still no way to compile.

SO !

Superpowered gives us a sample app to show how to mix Objective-C++ and use their lib within a Swift project (Here's a link to the git if you want https://github.com/superpoweredSDK/Low-Latency-Android-Audio-iOS-Audio-Engine).

Here is some screenshots of the sample project with what I understand and what I don't :

The Bridging-Header-File with the prototypes of the methods of the lib that I want to use within my Swift code (I don't like this but if it's the only way...).

The viewController file where the code in Swift is, and where I can create a Superpowered object thank's to the Bridging-Header-File, and call the methods that I've put in it.

And wtf I don't even understand why this Objective-C++ file is here and what it contains. It comes out from nowhere, not even their lib files.

So with this sample project in mind, I've created my own project, here is some screenshots :

The same Bridging-Header-File that in the sample project except that I include SuperpoweredIOSAudioIO.h so I can use SuperpoweredIOSAudioIODelegate.

My viewController file where the code in Swift is, and where I can create a Superpowered object thank's to the Bridging-Header-File, and call the methods that I've put in it.

Until here, it's great, except that I can't for exemple create a SuperpoweredRecorder object. If I try to include the SuperpoweredRecorder.h file in my Bridging-Header-File I have these errors :

So I saw that it is because SuperpoweredRecorder.h includes some .cpp files and I have to create a wrapper for cpp (a little bit like I did with the bridging header, no ?) but that includes a .h and a .mm file and I don't know what I have to put in that .mm file (the code of SuperpoweredRecorder.cpp ? But I don't have access to it)

So yes, I'm a little bit confused with all that stuff, can you help me to understand how can I use all the Superpowered lib in my Swift project please ?

解决方案

As I said in comment to @OmniProg, I had a little conversation with the CTO of Superpowered that helped me a lot to find the solution below.

So, as Swift cannot interact directly with C++ but can with Objective-C, I had to create objects in Objective-C++ (.mm file, a mix between C++ and Objective-C) that wrap C++ classes of the lib Superpowered.

Here is an example with the SuperpoweredRecorder object from the lib :

Here I create a .h file where I prototype my wrapper with the name SuperpoweredRecorderWrapped, and I also prototype in it all the methods of the SuperpoweredRecorder of the lib that I want to use.

Then I create a new .m file that I rename .mm and I implement SuperpoweredRecorderWrapped on it. I import both SuperpoweredRecorderWrapped.h and SuperpoweredRecorder.h. I create a SuperpoweredRecorder object as property named _wrapped and in my methods, I call the corresponding method of the _wrapped object. With that, when I'll call start of a SuperpoweredRecorderWrapped in my Swift code, this one will call start of _wrapped, a SuperpoweredRecorder object. See the trick ?

And finally I include all the wrapped classes in my Bridging-Header, like that I can instantiate my wrapped objects from Swift.

NOTE: All the C++ code HAVE to be in .mm files, that's why I make my #include of .h that contains C++ code in my .mm file and not in my .h file.

这篇关于如何在Swift项目中使用超级lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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