Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Swift 3 - Linux

You're reading from   Mastering Swift 3 - Linux Learn to build fast and robust applications on the Linux platform with Swift

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786461414
Length 380 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jon Hoffman Jon Hoffman
Author Profile Icon Jon Hoffman
Jon Hoffman
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Mastering Swift 3 - Linux
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Taking the First Steps with Swift FREE CHAPTER 2. Learning About Variables, Constants, Strings, and Operators 3. Using Swift Collections and the Tuple Type 4. Control Flow and Functions 5. Classes and Structures 6. Using Protocols and Protocol Extensions 7. Protocol-Oriented Design 8. Writing Safer Code with Error Handling 9. Custom Subscripting 10. Using Optional Types 11. Working with Generics 12. Working with Closures 13. Using C Libraries with Swift 14. Concurrency and Parallelism in Swift 15. Swifts Core Libraries 16. Swift on Single Board Computers 17. Swift Formatting and Style Guide 18. Adopting Design Patterns in Swift

Swift compiler


When we run our code using the swift command, or the Swift REPL tool, we are running the code as we would run a typical interpreted script such as a Bourne Again SHell (BASH) or Python script. This is really nice if we want to use Swift as a scripting language but if we want to build applications with Swift, we will need to compile our code into an executable form. To do this, we can use the Swift compiler.

Let's see how we would use the Swift compiler to build the Hello World example that we created earlier. If you recall from that example, we had to create a main.swift file. While using a main.swift file is not required for an application that only contains a single source file, it is required for applications that contain multiple source files. The Swift compiler will look for the main.swift and use it as the entry point for the application, similar to how a C compiler uses the main() function.

Tip

It is a good habit, when all of our code is contained in one file, to name that file main.swift.

To build the Hello World application that we created earlier, we would need to run the following command in the same directory that the main.swift file is in:

swiftc main.swift

This command should only take a second or so to run. Once it is completed, we should have an executable file named main. If your application contains multiple files, you can list them one by one as follows:

swiftc main.swift file1.swift file2.swift file3.swift

If you want to change the name of the output file, you can use the -o option as follows:

swiftc main.swift file1.swift file2.swift -o myexecutable

There are a number of command line options that you can use with the swift compiler. However, if you find that you are using a lot of command line options and making a complex compiler statement, you may want to look at the Swift Package Manager.

You have been reading a chapter from
Mastering Swift 3 - Linux
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786461414
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 $15.99/month. Cancel anytime
Visually different images