Book Image

Swift Cookbook. - Second Edition

By : Keith Moon, Chris Barker
Book Image

Swift Cookbook. - Second Edition

By: Keith Moon, Chris Barker

Overview of this book

Swift is an exciting, multi-platform, general-purpose programming language, and with this book, you'll explore the features of its latest version, Swift 5.3. The book begins with an introduction to the basic building blocks of Swift 5.3, its syntax, and the functionalities of Swift constructs. You’ll then discover how Swift Playgrounds provide an ideal platform to write, execute, and debug your Swift code. As you advance through the chapters, the book will show you how to bundle variables into tuples or sets, order your data with an array, store key-value pairs with dictionaries, and use property observers. You’ll also get to grips with the decision-making and control structures in Swift, examine advanced features such as generics and operators, and explore functionalities outside of the standard library. Once you’ve learned how to build iOS applications using UIKit, you'll find out how to use Swift for server-side programming, run Swift on Linux, and investigate Vapor. Finally, you'll discover some of the newest features of Swift 5.3 using SwiftUI and Combine to build adaptive and reactive applications, and find out how to use Swift to build and integrate machine learning models along with Apple’s Vision Framework. By the end of this Swift book, you'll have discovered solutions to boost your productivity while developing code using Swift 5.3.
Table of Contents (14 chapters)
12
About Packt

What this book covers

Chapter 1, Swift Building Blocks, introduces you to the basic building blocks of Swift 5, its syntax, and the functionalities of basic Swift constructs. Also, this chapter will introduce you to Apple’s Xcode IDE and Swift Playgrounds, which provides an ideal way to create, execute, debug, and understand the recipes contained in this book, thus setting you up to initiate the development process. In this chapter, you will learn how to write your first Swift program and understand the various basic elements of the Swift language.

Chapter 2, Mastering the Building Blocks, teaches you how to create more complex structures on the basis of the building blocks that you studied in the first chapter and the functionalities provided by the Swift standard library. You will get an understanding of how to bundle variables into tuples, order data with the help of an array, and store key-value pairs with dictionaries. You will also learn how to use the property observers and control the access to and visibility of your code. Then, you will learn how to extend the functionalities of your code using the extensions.

Chapter 3, Data Wrangling with Swift Control Flow, will teach you that programming is all about making decisions, therefore in this chapter, we will explore how to make decisions on the basis of the information gained and how to alter the control flow of the code. You will learn how to conditionally execute your code with if/else statements. You will also learn how to control the flow of execution of your code with switch statements, and then loop this code by understanding how to use for and while loops. You will then understand how to handle Swift errors with try, throw, do, and catch statements. Also, we will cover how a defer statement can be useful to change state once a function's execution is complete or to clean up values that are no longer needed.

Chapter 4, Generics, Operators, and Nested Types, provides you with an understanding of two advanced features of Swift, which are generics and operators. Using these features, you will learn how to build functionalities that are flexible and well defined. Also, you will understand how nested types allow logical grouping, access, and namespacing for your constructs.

Chapter 5, Beyond the Standard Library, takes you on a journey to explore the functionalities beyond the standard library provided by frameworks such as Foundation and UIKit. Learning how to use these functionalities will help you make full use of the Swift language.

Chapter 6, Building iOS Apps with Swift, is where we'll start the journey into building our very own iOS app. Taking everything we've learned and more from the previous chapters' recipes, this chapter will teach you how to get off the ground using Swift to build an iOS app.

Chapter 7, Swift Playgrounds, is where you will gain a total understanding of using Swift Playgrounds and explore advanced features other than those explored in previous chapters, to create fully interactive experiences.

Chapter 8, Server-Side Swift, introduces you to a totally different aspect of Swift programming: server-side programming with Swift. Also, you will gain an understanding of how to run Swift on Linux by installing the Swift toolchain. You will learn how to use a web server framework to build a REST API and how to host your API via a hosting service. You will also learn how to accomplish your tasks easily by understanding how to use Vapor, one of the most popular frameworks of Swift 5.

Chapter 9, Performance and Responsiveness in Swift, explores the more advanced concepts of Swift programming to provide an understanding of how certain Swift types are implemented and their performance characteristics. Also, you will learn how to perform asynchronous tasks using the Grand Central Dispatch API. Then, we'll explore the multi-threaded environment available on all Apple platforms and how to enhance the performance profile of your Swift constructs to build a fast and responsive app.

Chapter 10, SwiftUI and Combine Framework, dives straight into Apple's new UI framework, SwiftUI. With SwiftUI, we'll learn how to create iOS apps by exploring the declarative syntax used to achieve this. We'll also explore how easy it is for SwiftUI and UIKit to work together. In addition to SwiftUI, we'll also explore the basics of the new Combine framework and how this works alongside SwiftUI.

Chapter 11, Using CoreML and Vision in Swift, will dive into the frameworks offered by Apple and how we can use Swift code to harness machine learning techniques, as machine learning is becoming an increasingly hot topic. In this chapter, we'll cover CoreML and the Vision frameworks, using pre-trained models to identify objects in real time.