Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Compiling from the command line


I know that nowadays a lot of users and even developers think that using the command line is something from the past. The reality is that even today a lot of tasks that can be done from the command line, mainly automations tasks such as continuous integration, must be done using the command line.

This recipe will show you that it's not difficult, and better than this, you will have a better understanding of the concept about what Xcode does behind the scenes.

Tip

If you've never worked with a command line, I would suggest you read a book about it; Linux Shell Scripting Cookbook, Packt Publishing, is a good one in my opinion, even knowing that some commands are Linux specific.

Getting ready

Open a Finder window using the key combination command + Shift + U or open your Launchpad and click on the others folder. Here, you can see an icon called Terminal, open it and you should see a window similar to following one:

How to do it...

Here is how you can compile from the command line:

  1. Just type xcode-select -p; this should give to you one path, for example, /Applications/Xcode.app/Contents/Developer, If you don't have more Xcode versions installed in your machine, you shouldn't worry about the path, it will probably be right. If for any reason you have more than one Xcode installed on your machine, you will need to change it by typing xcode-select -s /Applications/XCODE VERSION.app/Contents/Developer.

    Note

    Remember that switching Xcode is a task that can only be done by an administrator, and it will affect every user.

  2. Now, go to your project directory and type xcodebuild -target "Chapter 1" -configuration Debug. After this, you will see lots of commands on screen, but the most important message is the last one that should be ** BUILD SUCCEEDED **; which means that the project was built without errors.

How it works...

When you type a command, your system will look for this command using the paths specified by the PATH variable. You can check the directories included in your PATH variable by typing echo $PATH . By default, the directory /usr/bin is included.

This directory contains Xcode commands, such as xcodebuild. When you want to use commands from other Xcode version, you need to use xcode-select to overwrite these files to use the ones according to the version you want.

Once you have set it, you can compile your project. As your project is a set of lots of files such as source codes, images, and so on, it would be hard work if we had to do every single action (compiling, copying files, code signing, and so on) one by one. This is the reason it's easier to ask Xcode to do it by himself using the command xcodebuild.

The xcodebuild command has a lot of parameters, so you can specify the configuration to be Debug or Release, the target you want to compile, as many other options. Type xcodebuild -help to get a list of options.

Tip

The -help argument is very common on Xcode commands. Try to use it when you have any doubt.

There's more...

Another good feature about the xcodebuild command is that it shows the commands that are being used with all its arguments. So, you can appreciate that when you compile an Objective-C project, Xcode uses the clang compiler, but when you have a Swift project, Xcode uses the swiftc command. Type swiftc -help with its full path to check its options and use them into the build options of other Swift flags.

Keep in mind that xcodebuild is going to look for a file called project.pbxproj, which is inside your .xcodeproj directory. This file contains information of every file, settings, and steps to create a project; in case of wrong syntax or wrong references, xcodebuild and the Xcode IDE won't compile the project at all. In addition to this fixing, this file could be a hard work. Because of these reasons, I wouldn't change this file manually, and also, I would try to avoid conflict with the Version Control System.