Episode #153

Feature Toggles

8 minutes
Published on January 22, 2015

This video is only available to subscribers. Get access to this video and 572 others.

In this episode I cover configuring your Xcode project to conditionally include or exclude a feature, in case there is a feature you can't have in the App Store, or perhaps something that isn't quite ready yet.

Update: As "Deny" says in the comments:

You can use preprocessor macros, but you must fill it in "Swift compiler - Custom Flags" section in project settings

Thanks! I was unaware of this setting.

Episode Links

Take a look at the configuration in the xcodeproj above for the settings. Then expose this configuration variable as a property in an Objective-C file:

// Features.h
#import <Foundation/Foundation.h>
const BOOL FeatureAEnabled;
#import "Features.h"

#if FEATURE_A
const BOOL FeatureAEnabled = YES;
#else
const BOOL FeatureAEnabled = NO;
#endif

Make sure you've enabled a bridging header so you can include this file:

// Bridging-Header.h
#import "Features.h"

Then you can use this in your Swift files:

class WindowController: NSWindowController {
    dynamic var featureAEnabled: Bool {
        return Bool(FeatureAEnabled);
    }    
}

And with this being a Mac app, you can bind your Hidden bindings to this property (using the NSNegateBoolean value transformer).

There are many ways of accomplishing this same technique. If you have a different (or better) way of handling this, please leave a comment.