Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
Swift Cookbook

Swift Cookbook

By : Costa
5 (1)
close
close
Swift Cookbook

Swift Cookbook

5 (1)
By: Costa

Overview of this book

If you are an experienced Objective-C programmer and are looking for quick solutions to many different coding tasks in Swift, then this book is for you. You are expected to have development experience, though not necessarily with Swift.
Table of Contents (13 chapters)
close
close
12
Index

Creating conditional code

Usually when we are developing, we have some cases where we would like to have different pieces of code according to our needs. For example, let's imagine that we would like to compare the performance of some functions written by us with some equivalent functions that were created on a third-party library. In this case, we can create some macros for using only our functions or for using only the third-party functions, allowing us to have the same application working in two different ways.

In this recipe, we will show how to create a log according to a platform and we can also enable or disable it if the execution is being affected by the excess of logs.

Getting ready

Create a new project called Chapter 1 Conditional Code, as shown earlier, and let's code a little bit.

How to do it...

To create a conditional code, follow these steps:

  1. After creating a new project, let's create a new file by navigating to File | New | File.... Now, choose Swift File and name it CustomLog.swift.

    Tip

    Don't save your files on a different folder from the project; this will give you problems in the future.

  2. Now, add the following code:
    func printLog(message: NSString){
        #if VERBOSE_LOG
            #if os(OSX)
                let OS="OS X"
            #else
                let OS="iOS"
            #endif
            
            #if arch(arm) || arch(arm64)
                let devicetype = "Mobile device"
            #elseif arch(x86_64) || arch(i386)
                let devicetype = "Computer"
            #else
                let devicetype = "Unkown"
            #endif
            
            NSLog("%@ on a %@ - %@", OS, devicetype, message)
        #endif
    }
  3. Now, go to the viewDidLoad method of your view controller, and add a call for this function, like this:
    printLog("Hello World")
  4. Try pressing play now; what do you see? The answer is—nothing! The reason is that the compiler knows nothing about the macro VERBOSE_LOG, which means that this macro is interpreted as false and the only thing that is created is an empty function.
  5. Now, go back to your project build settings, search for other swift flags, and add -DVERBOSE_LOG, as shown in the following screenshot:
    How to do it...
  6. Click on play again and you will see the log message.

How it works...

Currently, the Swift compiler has two defined macros: os() and arch(). The first one can receive OSX or iOS as argument, and the second one can receive x86_64, arm, arm64 and i386. Both macros will return a Boolean value. You can also create your own macro, defining it on your build settings.

The block that is evaluated as true will be compiled, and the other blocks will not be compiled; this way you can have code that calls OS-specific functions.

I would like to emphasize, mainly for those developers who are used to working with C projects, that the Apple documentation leaves a very clear message that Swift has no preprocessor; it only uses a trick on compilation time, so you can't use macros as we used to do on C or even on Objective-C. The only thing you can do is watch to see whether they are set or not.

There's more...

If you need, you can use the operators &&, ||, and ! as shown here: #if arch(arm64) && os(iOS), but you can't use any kind of comparator operator such as ==, <, and so on.

If you are interested in knowing more options that you can add to other Swift flags, check out the Compiling from the command line recipe in this chapter.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Swift Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon