Section I: Swift Basics
First, you’ll learn how computers and programming languages work, then lead into playgrounds, which you’ll use to run the code in this book. See how simple expressions, variables and constants work in Swift, then move on to how Swift handles types and operations. Learn more language fundamentals involving control flow, making decisions with conditionals and repeating things with loops. You’ll also learn to group procedures into functions, and how to use optionals to guard against cases where there may or may not be a value to work with.
Coding Essentials and Playground Basics
This is it, your whirlwind introduction to the world of programming! You’ll begin with an overview of computers and programming, and then say hello to Swift playgrounds, which are where you’ll spend your coding time for the rest of this book.
Expressions, Variables and Constants
You’ll start by learning some basics such as code comments, arithmetic operations, constants and variables. These are some of the fundamental building blocks of any language, and Swift is no different.
Types and Operations
You’ll learn about handling different types, including strings which allow you to represent text. You’ll learn about converting between types and you’ll also be introduced to type inference which makes your life as a programmer a lot simpler. You’ll learn about tuples which allow you to make your own types made up of multiple values of any type.
Basic Control Flow
You’ll learn how to make decisions and repeat tasks in your programs by using syntax to control the flow. You’ll also learn about Booleans, which represent true and false values, and how you can use these to compare data.
Advanced Control Flow
Continuing the theme of code not running in a straight line, you’ll learn about another loop known as the `for` loop. You’ll also learn about switch statements which are particularly powerful in Swift.
Functions are the basic building blocks you use to structure your code in Swift. You’ll learn how to define functions to group your code into reusable units.
This chapter covers optionals, a special type in Swift that represents either a real value or the absence of a value. By the end of this chapter, you’ll know why you need optionals and how to use them safely.
Section II: Collection Types
Why have one thing when you can have many? Learn about the various collection types available to you in Swift, and how to work with your collections through closures.
Arrays are the most common collection type you’ll run into in Swift. Arrays are typed, as are regular variables and constants and store multiple values like a single list.
Dictionaries are useful when you want to look up values by means of an identifier. For example, the table of contents of this book maps chapter names to their page numbers.
Collection Iteration with Closures
Swift has an object you can use to break up code into reusable chunks: a closure. These are particularly important when dealing with collections.
Section III: Building Custom Types
With the basics of Swift programming and collections under your belt, you can turn your attention to compound types in Swift. You’ll combine code and data into structures and classes, work with properties, learn how methods are different from functions, get started with classes, enumerate through collections, see how to implement protocols and learn how generics can help you out.
This chapter introduces structures, which are the first named type you’ll learn about. Structures are types that can store named properties and define their own behaviors.
In this chapter, you’ll learn about stored and computed properties, along with some tricks to deal with properties, how to monitor changes in a property’s value and delay initialization of a stored property.
Methods are merely functions that reside in a structure. In this chapter, you’ll take a closer look at methods and initializers.
Structures introduced you to your first named type, which lets you define your own named types. In this chapter, you’ll get acquainted with classes, which are much like structures.
This chapter introduces the finer points of classes and helps you understand how you can create, use and manage complex classes.
In this chapter, you’ll learn how enumerations work and when they’re useful. As a bonus, you’ll finally discover what an optional is under the hood.
Protocols are a type that can bridge common behaviors between structs, classes, and enums by defining an interface or template for an actual concrete type.
In this chapter, you’ll learn what generics are, learn how to write your own generic code, and loop back and look at the generic types in Swift - dictionaries, arrays, and optionals - from this new perspective.
Section IV: Beyond the Basics
Become a Swift master with advanced language features! Learn how to keep your code organized with public and private declarations, see how extensions work, and create your own custom operators and subscripts. Learn how to keep your code simple with pattern matching, manage real-world gotchas with error handling, discover the difference between value and reference types, and see how asynchronous closures and ARC work together to manage memory and avoid memory leaks. Finally, learn why Swift is referred to as a “protocol-oriented” language and how to extend protocols yourself.
Access Control and Code Organization
Swift gives you powerful tools for hiding complexity and organizing your code into easier to digest units. This chapter details how to do that.
Custom Operators and Subscripts
You’ll learn how you can define your own operators and subscripts to make your types feel even more like built-in language constructs.
With pattern matching you can accomplish more — with less typing. You’ll master all of its many forms in this chapter.
In the real world, some errors cannot be avoided. Handling them gracefully is what sets apart mediocre code from great code.
Asynchronous Closures and Memory Management
This chapter digs into the details of Swift memory management examining the relation between objects. It shows you how you avoid common leaks.
Value Types and Reference Types
Value semantics have a clear advantage over reference semantics in terms of the local reasoning but can lead to inefficiency for large objects. This chapter shows you how to get the best of both worlds.
From the standard library to user authored generics, Swift is a protocol-based language. In this chapter you’ll see how to get all of the benefits associated with object-oriented programming while being able to avoid most of the difficulties.