Mastering Swift 5.3 - Sixth Edition

Book description

A comprehensive guide for programming enthusiasts who wish to gain a firm command of the fundamentals and advanced Swift concepts

Key Features

  • Sixth edition of this bestselling book, improved and updated to cover the latest version of the Swift 5.3 programming language
  • Get to grips with popular and modern design techniques to write easy-to-manage Swift code
  • Use core Swift features such as concurrency, generics, and copy-on-write in your code

Book Description

Over the years, Mastering Swift has proven itself among developers as a popular choice for an in-depth and practical guide to the Swift programming language. This sixth edition comes with the latest features, an overall revision to align with Swift 5.3, and two new chapters on building swift from source and advanced operators.

From the basics of the language to popular features such as concurrency, generics, and memory management, this in-depth guide will help you develop your expertise and mastery of the language.

As you progress, you will gain practical insights into some of the most sophisticated elements in Swift development, including protocol extensions, error handling, and closures. The book will also show you how to use and apply them in your own projects. In later chapters, you will understand how to use the power of protocol-oriented programming to write flexible and easier-to-manage code in Swift. Finally, you will learn how to add the copy-on-write feature to your custom value types, along with understanding how to avoid memory management issues caused by strong reference cycles.

By the end of this Swift book, you will have mastered the Swift 5.3 language and developed the skills you need to effectively use its features to build robust applications.

What you will learn

  • Understand core Swift components, such as operators, collections, control flows, and functions
  • Identify how and when to use classes, structures, and enumerations
  • Use protocol-oriented design with extensions to write easy-to-manage code
  • Leverage design patterns with Swift to solve commonly occurring design problems
  • Apply copy-on-write for your custom value types to improve performance
  • Add concurrency to your applications using Grand Central Dispatch and operation queues
  • Implement generics to write flexible and reusable code

Who this book is for

This book is for beginners with a basic understanding of programming and experienced developers looking to learn Swift programming. Familiarity with Apple's tools will be beneficial but not mandatory. All examples should also work on the Linux and Windows platforms

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  2. Taking the First Steps with Swift
    1. What is Swift?
    2. Swift features
    3. Playgrounds
      1. Getting started with playgrounds
      2. iOS, tvOS, and macOS playgrounds
      3. Creating and displaying graphs in playgrounds
      4. What playgrounds are not
    4. Swift language syntax
      1. Comments
      2. Semicolons
      3. Parentheses
      4. Curly brackets
      5. An assignment operator does not return a value
      6. Spaces are optional in conditional and assignment statements
    5. Hello World
    6. Summary
  3. Swift Documentation and Installing Swift
    1. Swift.org
    2. Swift documentation
    3. Installing Swift from swift.org
    4. Building Swift and the Swift toolchain from source
      1. Installing dependencies
      2. Swift source
      3. Building Swift
      4. Installing Swift
      5. Testing the installation
    5. Using the Swift package manager
    6. Using the Swift compiler
    7. Summary
  4. Learning about Variables, Constants, Strings, and Operators
    1. Constants and variables
      1. Defining constants and variables
      2. Type safety
      3. Type inference
      4. Explicit types
    2. Numeric types
      1. Integer types
      2. Floating-point and Double values
    3. The Boolean type
    4. The String type
    5. Tuples
    6. Enumerations
    7. Operators
      1. The assignment operator
      2. Comparison operators
      3. Arithmetic operators
      4. The remainder operator
      5. Compound assignment operators
      6. The closed range operator
      7. The half-open range operator
      8. The ternary conditional operator
      9. The logical NOT operator
      10. The logical AND operator
      11. The logical OR operator
    8. Summary
  5. Optional Types
    1. Introducing optionals
    2. The need for optional types in Swift
    3. Defining an optional
    4. Using optionals
      1. Forced unwrapping of an optional
      2. Optional binding
      3. Optional types with tuples
      4. Optional chaining
      5. The nil coalescing operator
      6. Summary
  6. Using Swift Collections
    1. Swift collection types
      1. Mutability
    2. Arrays
      1. Creating and initializing arrays
      2. Accessing the array element
      3. Counting the elements of an array
      4. Is the array empty?
      5. Shuffling an array
      6. Appending to an array
      7. Inserting a value into an array
      8. Replacing elements in an array
      9. Removing elements from an array
      10. Merging two arrays
      11. Retrieving a subarray from an array
      12. Making bulk changes to an array
      13. Algorithms for arrays
        1. Sort
        2. Sorted
        3. Filter
        4. Map
        5. Count
        6. Diff
        7. forEach
      14. Iterating over an array
    3. Dictionaries
      1. Creating and initializing dictionaries
      2. Accessing dictionary values
      3. Counting the keys or values in a dictionary
      4. Is the dictionary empty?
      5. Updating the value of a key
      6. Adding a key-value pair
      7. Removing a key-value pair
    4. Set
      1. Initializing a set
      2. Inserting items into a set
      3. Determining the number of items in a set
      4. Checking whether a set contains an item
      5. Iterating over a set
      6. Removing items in a set
      7. Set operations
    5. Summary
  7. Control Flow
    1. What have we learned so far?
    2. Curly brackets
    3. Parentheses
    4. Control flow
    5. Conditional statements
      1. The if statement
        1. Conditional code execution with the if...else statement
      2. The guard statement
      3. The switch statement
        1. Match on wildcard
    6. Loops
      1. The for-in loop
        1. Using the for-in loop
      2. The while loop
        1. Using the while loop
        2. Using the repeat-while loop
    7. Using case and where statements with conditional statements and loops
      1. Filtering with the where statement
      2. Filtering with the for-case statement
      3. Using the if-case statement
    8. Control transfer statements
      1. The continue statement
      2. The break statement
      3. The fallthrough statement
    9. Summary
  8. Functions
    1. Using a single-parameter function
    2. Using a multi-parameter function
    3. Defining a parameter's default values
    4. Returning multiple values from a function
    5. Adding external parameter names
    6. Using variadic parameters
    7. inout parameters
    8. Omitting argument labels
    9. Putting it all together
    10. Summary
  9. Classes, Structures, and Protocols
    1. What are classes and structures?
      1. Similarities between classes and structures
      2. Differences between classes and structures
    2. Value versus reference types
    3. Creating a class or structure
    4. Properties
      1. Stored properties
      2. Computed properties
      3. Property observers
    5. Methods
      1. Custom initializers
      2. Internal and external parameter names
      3. Failable initializers
    6. Access controls
    7. Key-path expressions as functions
    8. Calling a type as a function
    9. Inheritance
      1. Overriding methods and properties
      2. Overriding methods
      3. Overriding properties
      4. Preventing overrides
    10. Protocols
      1. Protocol syntax
      2. Property requirements
      3. Method requirements
    11. Extensions
    12. Property wrappers
    13. Optional chaining
    14. Summary
  10. Protocols and Protocol Extensions
    1. Protocols as types
    2. Polymorphism with protocols
    3. Typecasting with protocols
    4. Protocol extensions
      1. A real-world example
    5. Do I need to use protocols?
    6. Adopting protocols using a synthesized implementation
    7. Swift's standard library
    8. Summary
  11. Protocol-Oriented Design
    1. Requirements
    2. Object-oriented design
    3. Protocol-oriented design
      1. Protocol inheritance
      2. Protocol composition
      3. Protocol-oriented design – putting it all together
      4. Using the where statement with protocols
    4. Structures versus classes
    5. Summary
  12. Generics
    1. Introducing generics
    2. Generic functions
    3. Generic types
      1. Conditionally adding extensions with generics
      2. Conditionally adding functions
      3. Conditional conformance
    4. Generic subscripts
    5. Associated types
    6. Summary
  13. Error Handling and Availability
    1. Native error handling
      1. Representing errors
      2. Throwing errors
    2. Catching errors
    3. Multi-pattern catch clauses
    4. The availability attribute
    5. Summary
  14. Custom Subscripting
    1. Introducing subscripts
    2. Subscripts with Swift arrays
      1. Creating and using custom subscripts
      2. Read-only custom subscripts
      3. Calculated subscripts
      4. Subscript values
      5. Static subscripts
      6. External names for subscripts
      7. Multidimensional subscripts
    3. Dynamic member lookup
    4. When not to use a custom subscript
    5. Summary
  15. Working with Closures
    1. An introduction to closures
    2. Simple closures
    3. Shorthand syntax for closures
    4. Using closures with Swift arrays
      1. Using closures with Swift's array algorithms
      2. Non-contiguous elements from an array
      3. Uninitialized arrays
    5. Changing functionality
    6. Selecting a closure based on results
    7. Summary
  16. Advanced and Custom Operators
    1. Bits and bytes
    2. Endianness
    3. Bitwise operators
      1. Printing binary numbers
      2. The bitwise AND operator
      3. The bitwise OR operator
      4. The bitwise XOR operator
      5. The bitwise NOT operator
      6. Bitwise shift operators
    4. Overflow operators
    5. Operator methods
    6. Custom operators
    7. Summary
  17. Concurrency and Parallelism in Swift
    1. Concurrency and parallelism
    2. Grand Central Dispatch (GCD)
      1. Calculation types
      2. Creating queues
      3. Creating and using a concurrent queue
      4. Creating and using a serial queue
      5. async versus sync
      6. Executing code on the main queue function
      7. Using asyncAfter
    3. Using the Operation and OperationQueue types
      1. Using BlockOperation
      2. Using the addOperation() method of the operation queue
      3. Subclassing the Operation class
    4. Summary
  18. Custom Value Types
    1. Value types and reference types
      1. Recursive data types for reference types
      2. Inheritance for reference types
      3. Dynamic dispatch
      4. Copy-on-write
    2. Implementing the Equatable protocol
    3. Summary
  19. Memory Management
    1. How ARC works
    2. Strong reference cycles
      1. Unowned references
      2. Weak references
    3. Summary
  20. Swift Formatting and Style Guide
    1. What is a programming style guide?
    2. API Design Guide
      1. Your style guide
        1. Do not use semicolons at the end of statements
        2. Do not use parentheses for conditional statements
        3. Naming
        4. Indenting
        5. Comments
        6. Using the self keyword
        7. Constants and variables
        8. Optional types
        9. Using type inference
        10. Using shorthand declarations for collections
        11. Using switch rather than multiple if statements
        12. Don't leave commented-out code in your application
    3. Summary
  21. Adopting Design Patterns in Swift
    1. What are design patterns?
    2. Creational design patterns
      1. The singleton pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the singleton pattern
      2. The builder pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the builder pattern
    3. Structural design patterns
      1. The bridge pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the bridge pattern
      2. The facade pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the facade pattern
      3. The proxy pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the proxy pattern
    4. Behavioral design patterns
      1. The command pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the command pattern
      2. The strategy pattern
        1. Understanding the problem
        2. Understanding the solution
        3. Implementing the strategy pattern
    5. Summary
  22. Other Books You May Enjoy
  23. Index

Product information

  • Title: Mastering Swift 5.3 - Sixth Edition
  • Author(s): Jon Hoffman
  • Release date: November 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781800562158