1

I want to use flags to control the compiler in Swift. Like we use #ifdef, #ifndef, #else, #endif in C (and C++, Objective C, ....)

I found the way to do it on the net, but I hit a problem in the following case. Anyone reading will understand what I want. Nevertheless the compiler complains. What is the way to go around? Of course without having to copy two times the same ten or more lines.

#if UseAds
class ViewController: UIViewController,XYZBannerDelegateProtocol {
#else
class ViewController: UIViewController {
#endif

Note that I got the information I am using here: http://en.swifter.tips/condition-compile/ which is similar to what can be found here.

But none of these solves my problem. They only tell me the basic way to do it.

4
  • Have you defined the UseAds value? Commented Oct 3, 2017 at 4:43
  • Possible duplicate of Swift 3: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys? Commented Oct 3, 2017 at 4:59
  • Yes I have defined the UseAds value. Commented Oct 3, 2017 at 5:04
  • This is not a duplicate. Please see my updated post. Commented Oct 3, 2017 at 5:12

1 Answer 1

2

You can use like this :

class ViewController: UIViewController {
    // Your common functions
}
#if UseAds
    extension ViewController: XYZBannerDelegateProtocol {
        // Your delegate methods    
    }
#endif
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.