Search code examples
c++iosobjective-cobjective-c++

What is the right syntax for adding Objective C @property into C++ class?


In my C++ project I have a base class for interstitial ad:

class InterstitialAd
{
public:

    virtual void initialize(std::string unit_id) = 0;

    virtual void load() = 0;

    virtual void show() = 0;

    virtual void uninitialize() = 0;
};

and need to implement it of iOS with Yandex Mobile Ads. According to their docs I need to add a @property and initialize it in my initialize method like this:

class YandexIosInterstitialAd : public InterstitialAd
{
    public:
        
        void initialize(std::string unit_id) override
        {
            //access interstitialAd here and add a code like this:

            self.interstitialAd = [[YMAInterstitialAd alloc] initWithAdUnitID:<your unique AdUnitId>];
            self.interstitialAd.delegate = self;
            [self.interstitialAd load];
        }

    private:
    
        //Need to declare this property 
        @property (nonatomic, strong) YMAInterstitialAd *interstitialAd;
}

what is the right syntax for that?

Ideally that @property should be declared inside my YandexIosInterstitialAd class, but not globally.


Solution

  • The property is not required to be an Objective-C property, but you anyway need an Objective-C class to conform to the YMAInterstitialAdDelegate protocol which the YMAInterstitialAd object apparently requires in order to initialize itself.

    Provided you compile the code with Objective-C++ syntax flag, you can just do the initialisation as follows:

    class YandexIosInterstitialAd : public InterstitialAd
    {
    public:
    
        YandexIosInterstitialAd(): delegate{ [TDWAdDelegate new] },
            interstitialAd{ [[YMAInterstitialAd alloc] initWithAdUnitID:<your unique AdUnitId>] } {
            interstitialAd.delegate = delegate;
            [interstitialAd load];
        }
    
    private:
    
        id<YMAInterstitialAdDelegate> delegate;
        YMAInterstitialAd *interstitialAd;
    
    };
    

    Be advised, that ARC knows how and when to release the Cocoa objects, even if they are C++ class members, so you don't commonly need to do it manually. TDWAdDelegate in this case is just another Objective-C class which implements the YMAInterstitialAd protocol and looks something like that:

    @interface TDWAdDelegate : NSObject <YMAInterstitialAdDelegate>
    
    - (void)interstitialAdDidLoad:(YMAInterstitialAd *)interstitialAd;
    - (void)interstitialAdDidClick:(nonnull YMAInterstitialAd *)interstitialAd;
    /* ...other methods which your implementation requires... */
    
    @end
    
    @implementation TDWAdDelegate
    
    - (void)interstitialAdDidLoad:(YMAInterstitialAd *)interstitialAd {
        // your implementation goes here
    }
    
    - (void)interstitialAdDidClick:(nonnull YMAInterstitialAd *)interstitialAd {
        // another method implementation
    }
    
    /* ...other methods which your implementation requires... */
    
    @end