Book Image

Swift 4 Programming Cookbook

By : Keith Moon
Book Image

Swift 4 Programming Cookbook

By: Keith Moon

Overview of this book

Swift 4 is an exciting, multi-platform, general-purpose programming language. Being open source, modern and easy to use has made Swift one of the fastest growing programming languages. If you interested in exploring it, then this book is what you need. The book begins with an introduction to the basic building blocks of Swift 4, its syntax and the functionalities of Swift constructs. Then, introduces you to Apple's Xcode 9 IDE and Swift Playgrounds, which provide an ideal platform to write, execute, and debug the codes thus initiating your development process. Next, you'll learn to bundle variables into tuples, set order to your data with an array, store key-value pairs with dictionaries and you'll learn how to use the property observers. Later, explore the decision-making and control structures in Swift and learn how to handle errors in Swift 4. Then you'll, examine the advanced features of Swift, generics and operators, and then explore the functionalities outside of the standard library, provided by frameworks such as Foundation and UIKit. Also, you'll explore advanced features of Swift Playgrounds. At the end of the book, you'll learn server-side programming aspect of Swift 4 and see how to run Swift on Linux and then investigate Vapor, one of the most popular server-side frameworks for Swift.
Table of Contents (9 chapters)

Introduction

Since Apple announced the Swift programming language at WWDC 2014, it has gone on to become one of the fastest growing programming language. TIOBE is a company that measures software quality and publishes a ranking index of programming language usage. At the time of writing, Swift ranks the 12th most popular language on this index and has overtaken Objective-C (visit http://www.tiobe.com/tiobe_index).

Swift is a modern, general purpose programming language that focuses on type safety, and an expressive and concise syntax. Seen as a replacement for the aging Objective-C, its use among Mac and iOS developers has skyrocketed, ensuring its place as the future of development on Apple's platforms.

While occupying this niche would alone ensure Swift's place as a useful and important programming language. Apple's decision to open source Swift's runtime and compiler have allowed Swift's influence to extend beyond Apple's ecosystem, giving it the potential to be used across all platforms and for any scenario.

Since open sourcing the Swift toolchain, Apple has provided support for running your Swift code on Linux. In the later chapters, we will investigate using a Swift server to execute your code. In addition, the release of the Swift Playgrounds iPad app, which happened alongside iOS 10, turns your tablet into a lightweight Integrated Development Environment (IDE). However, the simplest way to get up and running with Swift is still on Mac and with Apple's Xcode IDE. Therefore, this book will assume that this is also the development environment of the reader. Xcode also provides a perfect way to explore the structure and syntax of the Swift standard library, foundation, and any other framework available for iOS or Mac development in the form of its Playgrounds feature.

An Swift Playgrounds is a simplified environment for executing Swift code. For our purposes, Playgrounds provide an ideal way to create, execute, debug, and understand the recipes contained in this book. As such, it will also be assumed that the reader is using an Xcode Playground to implement the recipes contained in this book, unless otherwise stated.

Swift 3, released in 2016, presented a major step forward in standardizing the language syntax and, as a result, migrating code written in Swift 2 to Swift 3 was not always an easy task. Swift 4, by contrast, has been designed to be source compatible with Swift 3; therefore, the task of migrating between Swift 3 and Swift 4 should be minimal. Swift 4 has been available in a prerelease form since the beta release of Xcode 9, and will be finalized with the release of Xcode 9 and iOS 11. This book will use Swift 4 throughout, and differences from Swift 3 will be highlighted.

In this chapter, we will look at the building blocks of the Swift language, examining the syntax and functionality of the basic Swift constructs that everything else is based on.

All the code for this chapter can be found in GitHub repository at https://github.com/SwiftProgrammingCookbook/SwiftBuildingBlocks.