使用Swift导入aurioTouch库 [英] Import aurioTouch Library with Swift

查看:81
本文介绍了使用Swift导入aurioTouch库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图将Objective-C应用程序转换为Swift。


我想在


如果到目前为止(建议),并且已添加:



  • Accelerate.framework

  • AudioToolbox.framework

  • AVFoundation.framework


Build Phases 中的目标并进行编译,您会发现它已建立并链接。


包装


我花了1小时47分钟到达了这一点(下面有证据)。下一步当然是将包装器代码实际放在 AudioControllerBridge 中,以便返回(包装):

  BufferManager * _bufferManager; 
DCRejectionFilter * _dcRejectionFilter;
-(BufferManager *)getBufferManagerInstance;

这是3个非 Objective-C 元素 class


包装 BufferManager 和<$可能更干净c $ c> DCRejectionFilter ,以便可以在 Swift 中自由使用它们。我将把这个决定交给读者。




Demo


仅作记录,以上所有说明均已导致

  let ac = AudioControllerBridge()

的成功编译b $ b

如该屏幕截图所示。它会显示所有文件,并显示在iOS 9上适用于iPhone 6的Xcode 7上的成功构建。



I'm trying to convert an Objective-C app to swift.

I'd like to import classes in aurioTouch to Swift app, so I created the following Bridging-Header file:

#import "AudioController.h"

but I received following errors in DCRejectionFilter.h, BufferManager.h, FFTHelper.h:

Unknown type name 'class'; did you mean 'Class'?

Expected ';' after top level declarator

and also in AudioController.h:

Unknown type name 'BufferManager'

Unknown type name 'DCRejectionFilter'

Of course I use .mm instead of .m, but this does not work.

update

The simple swift project just including aurioTouch Library (with Obj-C and C++) is as follows: https://github.com/pika-shi/aurioTouch-Sample

解决方案

The present answer shows you how to solve the Bridging Header #import, and is a step-by-step tutorial on how to create an Objective-C wrapper object.

.mm does not mean Swift

.mm does not mean Objective-C either

It means Objective-C++, and merely renaming a file to .mm offers no improvement. Notice that you are still including the same .h files, and those are where the problem starts. These .h reference C++ classes, and they must be wrapped.

Wrap the C++ in Objective-C

The file AudioController.h is not an Objective-C file: it includes BufferManager.h which is a C++ file, and compilation stops right there.

You need to create a true wrapper, say AudioControllerBridge which .h is in Objective-C, and .mm can, in turn, make references to C++:

.h

Absolutely, positively no C++ allowed, explicit, included, or else.

#import <Foundation/Foundation.h>
@interface AudioControllerBridge : NSObject
    // ...
@end

.mm

Objective-C++ tolerates all the C++ you need, as long as it is not exposed in the interface.

#import "AudioControllerBridge.h"
#import "AudioController.h"
@implementation AudioControllerBridge
    // ...
@end

Of course, you could simply modify the AudioController.h directly, but we will consider this bad practice: for the rest of this answer, we will assume that you are attempting to integrate aurioTouch as-is, with exactly 0 lines of code changed.

In the implementation of AudioControllerBridge, you can now instantiate the AudioController, and import all the C++ files you need for proper compilation, something you could not do in the .h. Remember that the .h you are exposing to Swift in Briding-Header must be a pure Objective-C interface.

// Bridging Header
#import "AudioControllerBridge.h"

ARC

You will soon see that you need to download CoreAudio/PublicUtility because some files, like CADebugPrintf are simply missing from the example, and somehow will not build in your new project, at least in DEBUG mode.

If you made it so far, you will then find out that you will get a dozen deprecated warnings, which you can ignore for now, and half as much ARC errors in AudioController.mm. Fix with -fno-objc-arc Compiler Flag:

If you made it so far (props), and have added:

  • Accelerate.framework
  • AudioToolbox.framework
  • AVFoundation.framework

to your target in Build Phases and compiled, you will find that it builds and links.

Wrapping it up

It took me 1h 47mins to reach that point (proof below). The next step is of course to actually put the wrapper code in AudioControllerBridge, so that it returns (wraps):

BufferManager*          _bufferManager;
DCRejectionFilter*      _dcRejectionFilter;
- (BufferManager*) getBufferManagerInstance;

which are the 3 non-Objective-C elements in that class.

It may be cleaner to to wrap BufferManager and DCRejectionFilter as well, so that they can be used freely in Swift. I will let that decision to the reader.


Demo

Just for the record, all the instructions above lead to a successful compilation of

let ac = AudioControllerBridge()

as seen in this screenshot. It reveals all the files needed, and show a successful build on Xcode 7 for iPhone 6 on iOS 9.

这篇关于使用Swift导入aurioTouch库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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