35

How can I differentiate between two Xcode targets with Swift? The idea is to make a free and a paid version of an app with the same code base in Xcode.

With objective C I could use preprocessor macros but with Swift those are unavailable.

3 Answers 3

47

In Xcode, go into the build configuration for a target. Find the section called Swift Compiler - Custom Flags, which contains a setting called Other Swift Flags.

Add a command-line flag for the compiler to add a flag, pretty much just like you’d do with the C compiler.

Swift Compiler Flags

Now you’ve got -D Something being passed to the Swift compiler. In your Swift code, you can now do this:

#if Something
    let foo = "bar"
#endif

It looks a lot like the C preprocessor, but unlike C, all code in all conditional sections has to be syntactically correct or the program won’t compile. So, you can set a flag on each target in the build settings and use them in your code.

5
  • 4
    More importantly, all code in an #if, active or no, must also be a complete syntactic unit. So you can't, say, put an #if/#else/#endif around just a func declaration and leave the function body outside the #if.
    – rickster
    Dec 4, 2014 at 2:17
  • is this work in storyboard ,,I want to change image based on target in storyboard Jul 25, 2016 at 11:49
  • I don't get it. shouldn't it be if Something == QA else if Something == Stage`... The way you wrote it, it seems that the existence of that flag is all that's necessary. What would happen if two builds have the same flag?!
    – mfaani
    Feb 6, 2018 at 21:03
  • If you need to test multiple values, then yes. This is just a straight boolean.
    – Jeremy
    Mar 6, 2018 at 18:00
  • Is this will work if I need to include a resource [either a swift file or nib] in one target and not in others.
    – Shyam
    Nov 12, 2018 at 6:13
20

Since Xcode 8 you can set the compilation conditions in the Build Settings for each target under Active Compilation Conditions.

Active Compilation Conditions Screenshot

With those set you can use:

#if FREE
   //do something
#endif

For more details see the i40west answer and comments.

4

One way is to add Flags to target and use preprocessor. But the other way I think would be to use something like this

if Bundle.appTarget == "" { } else { }


extension Bundle {

    public static var appVersion: String? {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
    }

    public static var appBuild: String? {
        return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
    }

    public static func _version() -> String {
        let dictionary = Bundle.main.infoDictionary!
        let version = dictionary["CFBundleShortVersionString"] as! String
        let build = dictionary["CFBundleVersion"] as! String
        return "\(version) build \(build)"
    }

    public static var appTarget: String? {
        if let targetName = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String {
            return targetName
        }
        return nil
    }
}
1
  • 1
    You can also use Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") this keys should be defined in Info.plist Sep 26, 2019 at 8:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.