How can you conditionally include protocols at compile time?

Is it possible to conditionally enable the protocol? For example, below is code that doesn't work, but should give you an idea of ​​what I mean. I want the AdWhirlDelegate interface to be enabled if ads are enabled.

// this works fine #if ADS_SUPPORTED #import "AdWhirlView.h" #endif // this does NOT work @interface MyAppDelegate : NSObject <UIApplicationDelegate #if ADS_SUPPORTED ,AdWhirlDelegate #endif> 
0
source share
2 answers

You can do:

 #if ADS_SUPPORTED @interface MyAppDelegate : NSObject <UIApplicationDelegate,AdWhirlDelegate> #else @interface MyAppDelegate : NSObject <UIApplicationDelegate> #endif 

... but then IB can get a little confused. See this answer for an alternative to this.

+2
source

This will not work

Repeat the interface declaration twice, one with the protocol and one without locking inside the structure # if / # else / # endif

Preprocessor directives will only work in a non-nested manner, like your header.

0
source

Source: https://habr.com/ru/post/891836/


All Articles