Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using compiler variables in Swift

In Objective-C I had a bunch of compiler flags set in Build Settings -> Other C Flags that were being used in the code. For instance:

Flag => -DPortNumber = 1

And in code I was able to access it by @(PortNumber)

This doesn't work in Swift, and I'm not able to find an answer.

like image 866
aryaxt Avatar asked Jan 06 '15 17:01

aryaxt


People also ask

How to declare and use variables in Swift 4 programming?

The following section will cover how to declare and use various types of variables in Swift 4 programming. A variable declaration tells the compiler where and how much to create the storage for the variable. Before you use variables, you must declare them using var keyword as follows −

What are compiler directives in Swift?

Perhaps the most commonly used Swift compiler directive is the #if command, which enables us to conditionally include or exclude certain code blocks when our program is being compiled.

How to declare a constant in Swift?

A constant is a special type of variable whose value cannot be changed. For example, Here, after a is initialized to 5, we cannot change its value. In Swift, we use the let keyword to declare constants. The value of a constant cannot be changed. For example, Also, you cannot declare a constant without initializing it.

How do you change the name of a variable in Swift?

If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with backticks ( `) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice. You can change the value of an existing variable to another value of a compatible type.


Video Answer


1 Answers

The -D flag to C compilers defines a preprocessor macro. There are no preprocessor macros in Swift. So if you're looking to do something like:

// compile with -DPORT_NUMBER 31337
var port = PORT_NUMBER    // error

... you can't. Swift is designed for source code to be syntactically complete before compilation. If you could switch out blocks of it at build time, you'd break the ability of the toolchain to help verify that your code is correct. (Partly this is because preprocessor macros in C are textual replacement: you can use them to rewrite any part of the language, not just fill in values for variables.)

The Swift compiler does have a -D flag, but its use is more limited: you can use it for build configurations only. So, if you wanted to do something like the following, you'd be cool:

// compile with -DUSE_STAGING_SERVER
#if USE_STAGING_SERVER
var port = 31337
#else
var port = 80
#endif

Note that unlike C, everything inside an #if block needs to be syntactically complete. (For example, you can't put just the declaration line of a func in an #if block and leave the function body outside the conditional.)

Of course, this doesn't help you if you want to have a configuration value set at compile time be used in your code. For that, I'd recommend alternate approaches. Xcode can still do textual substitution in resource files, like property lists. (Note that the Info.plist that comes with your app is full of things like $(TARGET_NAME), for example.) So, you could include a bundle resource with your app whose contents are populated at compile time according to your project settings, then read your port number from that.

like image 116
rickster Avatar answered Oct 24 '22 23:10

rickster