How to use Superpowered lib in Swift project

前端 未结 3 2148
迷失自我
迷失自我 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 04:00

    I haven't programmed in Objective-C++, but I do have experience with C, C++, Objective-C, and Swift, so here are some observations and ideas based on looking at the Superpowered SDK and sample code.

    A bridging header lets Swift interface directly with Objective-C, and since Objective-C is a strict superset of C, this implies interfacing with C as well. However, you cannot interface directly with C++, and that's where Objective-C++ comes to the rescue. From what I'm seeing you can mix Objective-C and C++ code in Objective-C++, which allows Objective-C to use C++ classes.

    Now on to some specifics.

    The bridging header in the SuperpoweredFrequencies example that you looked at introduces a new Objective-C class, Superpowered, which is not part of the library, but of the example, and is implemented in Superpowered.mm. It is an Objective-C++ file, because Superpowered calls some C++ code.

    Looking at Superpowered.mm, you will see that it imports Objective-C headers:

    #import "SuperpoweredFrequencies-Bridging-Header.h"
    #import "SuperpoweredIOSAudioIO.h"
    

    as well as C and C++ headers:

    #include "SuperpoweredBandpassFilterbank.h"
    #include "SuperpoweredSimple.h"
    

    We could have used import instead of include for C++ code, too, but they are probably using include just to emphasize that it is C++ code. Looking at SuperpoweredBandpassFilterbank.h, we see that SuperpoweredBandpassFilterbank is a C++ class. It is used in Superpowered.mm by the Superpowered Objective-C++ class, see the filters member, which is a pointer to a SuperpoweredBandpassFilterbank object.

    In the project you attempted to build, I see where the Superpowered interface is declared in the bridging header, but I don't see an implementation. The failure to #import SuperpoweredRecorder.h is due to SuperpoweredRecorder.h being a C++ header. The #include should be in your Objective-C++ (.mm) file, it's useless in the bridging header, since Swift can't make sense of C++ anyhow.

    Hopefully this is helpful. Welcome to the world of C++.

提交回复
热议问题