Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Cookbook

You're reading from   Swift Cookbook Over 50 hands-on recipes to help you create apps, solve problems, and build your portfolio of projects in Swift

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781784391379
Length 392 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Xcode and Swift FREE CHAPTER 2. Standard Library and Collections 3. Using Structs and Generics 4. Design Patterns with Swift 5. Multitasking 6. Playground 7. Swift Debugging with Xcode 8. Integrating with Objective-C 9. Dealing with Other Languages 10. Data Access 11. Miscellaneous 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:

Getting ready

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.

You have been reading a chapter from
Swift Cookbook
Published in: Apr 2015
Publisher:
ISBN-13: 9781784391379
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image