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

Using the Swift Package Manger


According to Apple, the Swift Package Manager is defined as:

A tool for managing the distribution of Swift code. It's integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

The first part of the previous statement means that the Swift Package Manager can be used to manage the distribution of modules. Some applications may have all their code organized into a single module. More complex applications may separate their code into different modules. As an example, if you were developing an application that communicates with another device over Bluetooth, you may want to put the Bluetooth code into a separate module, so you can reuse it in other applications.

The Swift Package Manager will manage module or application dependencies and will automate the process of downloading, compiling, and linking these dependencies. This allows us to concentrate on our code rather than figuring out how to write complex build scripts to compile our applications or modules.

A package consists of the Swift source files and a manifest file called Package.swift. The manifest file defines the package's name and contains instructions on how to build the package. We can customize the manifest file to declare build targets or dependencies, include or exclude source files, and specify build configurations for the module or application.

Let's look at how we can use the Package Manager to create a simple application. Firstly, we need to decide how we will name the application. For our example, we will use the name PMExample. Let's create a directory with that name and then change to that directory:

mkdir PMExample
cd PMExample

Now, we need to build the framework that the Package Manager needs. To do this, we will run the following command:

swift package init

This command will create both the Sources and Tests directory. It will also create a number of files including the Package.swift manifest file and also a file in the Sources directory with the same name as your application. If you look at the Package.swift manifest file, it should look as follows:

import PackageDescription 
let package = Package( name: "PMExample" ) 

This is the most basic manifest file, and it simply defines the name for the package. We will look at this manifest file in greater depth in Chapter 13, Using C Libraries with Swift.

Now, let's look at the PMExample.swift that was created for us in the Sources directory. It should contain code similar to the following code:

struct PMExample {       
   var text = "Hello, World!" 
} 

Let's add some code to this file, so it contains the following code:

struct PMExample {       
   var text = "Hello, World!" 
   func sayHello() { 
      print(text) 
   } 
} 

All we did in this example was add a method that prints Hello, World! to the console.

Tip

Don't worry if you do not understand this code yet. We are looking to get you familiar with the Package Manager and the compiler, so you feel comfortable compiling the code examples in this book.

Now let's add the main.swift file to the Sources directory. This will be the entry point to our application. Add the following code to this file:

let example = PMExample() 
example.sayHello() 

Now, go back to the PMExample directory, and run the following command to build the PMExample application.

swift build

This will build the application. If all is well, we will have an executable application in the PMExample/.build/debug directory named PMExample.

Note that, in the PMExample.swift file, all of the code was contained in the PMExample structure, whereas the code in the main.swift file was top-level code. Remember what we noted earlier: the main.swift file is the only file that can contain top-level code.

We only scratched the surface of what the Package Manager can do in this section. We will look at it further in Chapter 13, Using C Libraries with Swift.

When all of the source code is in one file, it will be much easier to use the swift compiler to build your executable code; however, once your application grows past that single source file, I would recommend looking at the Package Manager to manage your dependencies and your builds.

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