In Part 1 we learnt how to install Swift 3.0 on our Raspberry Pi. In this part we will learn how to use the Swift-Lite system to add Swift Modules and add extra functionality to our "Hello, swifty world" project.
For more information about Swift-Lite please go to www.swift-lite.org.
What is a Swift Module?A Swift Module is simply a .swift file that can be re-used in many projects.
Swift Modules can be used in much the same way you would use a Framework or a Library.
Swift-Lite uses meta-tags combined with the pre-build processor to make constructing a multi-file Swift project with Module dependancies easy and simple.
Lets get started!
Swift-Lite File Types.Swift-Lite has 2 file types, both types are standard Swift code files with the .swift extension.
The Project FileThe Project file is the "main" file for the project. The Project file is identified by the Swift-Lite system by adding a "type" meta tag of "project" to the header comment section. We also include a "name" meta tag.
// A Swift-Lite Project File
// type:project
// name:helloworld
The Module FileThe Module file contains code that we want to use in the main project. Modules make it easy to share code between many different projects without the need to copy and paste. A good example of this is the "GPIO module". This module contains all the code we need to access the GPIO pins on the Raspberry Pi. If we need to use the GPIO pins we simply "include" the GPIO module file.
The Module file is identified by the Swift-Lite system by adding a "type" meta tag of "module" to the header comment section. We also include a "name" meta tag.
// A Swift-Lite Module File
// type:module
// name:swiftGPIO
How to Include a ModuleTo use a Module file in a project we simple add an "include" meta tag with the module file name to the Project file header comment section.
// A Swift-Lite Project File
// type:project
// name:helloworld
// include:swiftGPIO.swift
Directory StructureIn part 1 we created a projects directory called "swiftProjects". Now we need to create a directory for our Swift Modules. The location and name of this directory is important as this is where "swift-lite-build" looks for your module files.
In your home/user directory create a new directory called "swiftModules".
mkdir swiftModules
In our home/user directory we should now have a "swiftProjects" and a "swiftModules" directory.
As the names imply, projects go in "swiftProjects" and modules go in "swiftModules".
FoundationThe Foundation framework defines a base layer of functionality that is required for almost all applications. It provides primitive classes and introduces several paradigms that define functionality not provided by the language or runtime.
Adding a Swift ModuleWe are going to create a module called "date.swift". The purpose of the module is to return the current date by using "Date" from the "Foundation" framework.
Change to the "swiftModules" directory.
cd swiftModules
Open the nano text edit and create a new swift file called "date.swift".
nano date.swift
Add the following code.
GNU nano 2.2.6 File: date.swift
// A Swift-Lite Module File
// type:module
// name:date
import Foundation
func printToday(){
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full
let dateString = dateFormatter.string(from: date as Date)
print("FullStyle Date Format = \(dateString)")
}
In the code we have created a function (func) called "printToday". This is the function we will be calling from the main project file "helloworld.swift". Save the file and exit the editor. We now have finished the module file.
Change to the "swiftProjects" directory.
cd
cd swiftProjects
Open "helloworld.swift" in the text editor.
nano helloworld.swift
Add the following code.
GNU nano 2.2.6 File:
// A Swift-Lite Project File
// type:project
// name:helloworld
// include:date.swift
print("Hello, swifty world!")
// function from date.swift module
printToday()
The most important part of this code is the "include" meta tag. The "include" tag tells the Swift compiler to include the module file in the build. The "include" tag must have the exact and full file name of the module. The module file must be in the "swiftModules" directory.
Now we can call the "printToday()" function from the "date.swift" module file.
The "date module" can now be used in any project by simply adding the "include:date.swift" meta tag in the header comments section.
Save the file and exit the editor. We now have finished the project file.
Build the ProjectWe build the project by using "swift-lite-build" followed by the project name.
swift-lite-build helloworld.swift
Processing Files
Building!
Build Finished
Run the swapp.
./helloworld.swapp
Hello, swifty world!
FullStyle Date Format = Saturday, October 15, 2016 at 2:03:19 AM GMT
SummarySwift-Lite provides an easy way to create and build multi-file Swift projects and allow the easy re-use of Swift Modules across many different projects.
NextIn the next project we will learn how to use the Raspberry Pi GPIO pins and flash an LED.
Comments
Please log in or sign up to comment.