Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 351 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Differentiate between two Xcode targets with Swift

#1
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.
Reply

#2
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
}
}
Reply

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

[![Active Compilation Conditions Screenshot][1]][1]

With those set you can use:

#if FREE
//do something
#endif

For more details see the [i40west answer][2] and comments.


[1]:

[2]:

[To see links please register here]

Reply

#4
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][1]


[1]:


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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through