How to use Superpowered lib in Swift project

前端 未结 3 2079
迷失自我
迷失自我 2021-02-14 03:21

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 s

3条回答
  •  逝去的感伤
    2021-02-14 03:41

    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.

提交回复
热议问题