Head First Swift

Book description

What will you learn from this book?

Swift is best known as Apple's programming language of choice for developing apps on iOS, iPadOS, macOS, watchOS, and tvOS. But it's far more versatile than that. Open source Swift is also gaining ground as a language for systems programming and server-side code, and it runs on Linux and Windows. So where do you start? With Head First Swift, you'll explore from the ground up: from collecting and controlling data to reusing code, producing custom data types, and structuring programs and user interfaces with SwiftUI by building safe, protocol-driven code. With Swift under your belt, you'll be ready to build everything from mobile and web apps to games, frameworks, command-line tools, and beyond.

What's so special about this book?

If you've read a Head First book, you know what to expect--a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With this book, you'll learn Swift through a multisensory experience that engages your mind rather than a text-heavy approach that puts you to sleep.

Table of contents

  1. Authors of Head First Swift
  2. Table of Contents (the real thing)
  3. how to use this book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did
    6. Here’s what YOU can do to bend your brain into submission
    7. Read me
    8. The technical review team
    9. O’Reilly Online Learning
    10. Acknowledgments
  4. 1. Introducing Swift: Apps, Systems, and Beyond!
    1. Swift is a language for everything
    2. The swift evolution of Swift
    3. Swift into the future
    4. How you’re going to write Swift
    5. The path you’ll be taking
    6. Getting Playgrounds
      1. Downloading and installing Playgrounds on macOS
    7. Creating a Playground
    8. Using a Playground to code Swift
    9. Basic building blocks
    10. A Swift example
    11. Here’s what you need to build
    12. The print function
    13. 1 Building a list of ingredients
    14. 2 Picking four random ingredients
    15. 3 Displaying our random pizza
    16. Congrats on your first steps with Swift!
    17. Swiftcross
    18. Swiftcross Solution
  5. 2. Swift by Name: Swift by Nature
    1. Building from the blocks
    2. Basic operators
    3. Operating swiftly with mathematics
      1. Express yourself
    4. Expressing yourself
    5. Names and types: peas in a pod
    6. Not all data is numbers
      1. These are all types
    7. Swift Safety
    8. Stringing things along with types
    9. Who am I?
    10. String interpolation
    11. Swiftcross
    12. Swiftcross Solution
    13. Who am I Solution?
  6. 3. Collecting and Controlling: Going Loopy for Data
    1. Sorting pizzas
    2. Swift collection types
    3. Collecting values in an array
    4. How big is that array, exactly? Is it empty?
    5. Collecting values in a set
    6. Collecting values in a dictionary
    7. Tuples
    8. Everyone needs a good alias
    9. Control flow statements
    10. if statements
    11. switch statements
    12. Building a switch statement
      1. Start with the switch keyword
    13. Range operators
    14. More complex switch statements
    15. Getting repetitive with loops
    16. Building a for loop
    17. Building a while loop
    18. Building a repeat-while loop
    19. Solving the pizza-sorting problem
    20. Phew, that’s a lot of Swift!
    21. Code Magnets
    22. Pool Puzzle
    23. BE the Swift Compiler
    24. Swiftcross
    25. Code Magnets Solution
    26. Pool Puzzle Solution
    27. BE the Swift Compiler Solution
    28. Swiftcross Solution
  7. 4. Functions and Enums: Reusing Code on Demand
    1. Functions in Swift let you reuse code
      1. What can functions do?
    2. Built-in functions
    3. What can we learn from built-in functions?
    4. Improving the situation with a function
    5. Writing the body of the function
    6. Using functions
    7. Functions deal in values
    8. Many happy returns (from your functions)
      1. But Swift is meant to make things simple...
    9. A variable number of parameters
    10. Fireside Chats
    11. What can you pass to a function?
    12. Every function has a type
    13. Function types as parameter types
    14. Multiple return types
    15. Fireside Chats
    16. Functions don’t have to stand alone
      1. Nested functions
    17. Switching with enums
    18. Functioncross
    19. Functioncross Solution
  8. 5. Closures: Fancy, Flexible Functions
    1. Meet the humble closure
    2. Closures are better with parameters
    3. Boiling it all down to something useful
      1. The problem
      2. The solution
    4. Reducing with closures
    5. Capturing values from the enclosing scope
      1. What came first, the function or the closure?
    6. Escaping closures: the contrived example
    7. Autoclosures provide flexibility
      1. The situation
      2. The problem
      3. The solution
    8. Shorthand argument names
    9. Closurecross
    10. BE the Swift Compiler
    11. Closurecross Solution
    12. BE the Swift Compiler Solution
  9. 6. Structures, Properties, and Methods: Custom Types and Beyond
    1. Let’s make a pizza, in all its glory...
      1. A Pizza struct
    2. Under the hood...
    3. The initializer behaves just like a function
      1. Access control and structs
    4. Methods inside structures
    5. Changing properties using methods
    6. Computed properties
      1. Creating a property observer
    7. Code Magnets
    8. Getters and setters for computed properties
    9. Implementing a setter
    10. Swift Strings are actually structs
    11. The case for lazy properties
    12. Using lazy properties
    13. Structcross
    14. Structcross Solution
    15. Code Magnets Solution
  10. 7. Classes, Actors, and Inheritance: Inheritance Is Always a Thing
    1. A struct by any other name (that name: a class)
    2. Inheritance and classes
    3. Replacing Overriding methods
      1. Here’s our Plant class:
      2. Here’s our Succulent child class, overriding a method of the parent:
    4. Under the hood...
    5. Final classes
    6. Automatic reference counting
    7. Mutability
      1. Beware
    8. Swift Safety
    9. Fireside Chats
    10. BE the Swift Compiler
    11. Pool Puzzle
    12. Classcross
    13. Pool Puzzle Solution
    14. BE the Swift Compiler Solution
    15. Classcross Solution
  11. 8. Protocols and Extensions: A Swift Lesson in Protocol
    1. The Robot Factory
      1. The problem:
      2. A solution:
    2. Protocol inheritance
    3. Mutating methods
    4. Protocol types and collections
    5. Computed properties in extensions
      1. Useful extensions and you
      2. Adding an initializer (with an extension)
    6. Extending a protocol
    7. Useful protocols and you
      1. Protocol composition
    8. Conforming to Swift’s protocols
      1. Sequences
      2. Conforming to Sequence and IteratorProtocol:
      3. Using your new sequence:
      4. Equatable
      5. The problem
      6. Conforming to Equatable:
      7. Implementing ==
    9. Code Magnets
    10. Protocolcross
    11. BE the Swift Compiler
    12. Code Magnets Solution
    13. Protocolcross Solution
    14. BE the Swift Compiler Solution
  12. 9. Optionals, Unwrapping, Generics, and More: Nothing Optional About It
    1. Optionals Exposed
    2. Dealing with something that’s missing
      1. The problem
    3. Why you might need an optional
      1. The struct for Josh is good, it represents Josh reasonably well: Josh is named “Josh”, and they’ve consumed 5 coffees.
      2. But the struct for Tom doesn’t really represent them. They haven’t really consumed 0 coffees; they don’t drink it at all.
    4. Optionals and handling missing data
      1. A Person
      2. Alex and Tom
    5. Unwrapping optionals
    6. Unwrapping optionals with guard
      1. Using guard let to unwrap an optional:
    7. Force unwrapping
    8. Swift Safety
    9. BE the Swift Compiler
    10. Swift Safety
    11. Generics
      1. Queue requirements
    12. A queue with generics
    13. Here’s our new Queue type
      1. Using your Queue
    14. Optionalcross
    15. Optionalcross Solution
    16. BE the Swift Compiler Solution
  13. 10. Getting Started With Swiftui: User Interfaces…Swiftly
    1. What’s a UI framework, anyway?
    2. Your first SwiftUI UI
    3. UI building blocks
    4. Making a list, checking it…quite a few times, to get it perfect
    5. User interfaces with state
    6. Buttons are for pressing
    7. Let’s see how far you’ve come
    8. Customizing your views with modifiers
      1. Using view modifiers:
      2. Order matters (sometimes):
    9. Create a new SwiftUI Xcode project, for iOS
    10. Your Xcode will look something like this
    11. Create a new type to store a todo item in
    12. Make sure each todo item can be uniquely identified
    13. Create a user interface for the app
      1. What’s going on in this user interface?
    14. Create a user interface for the app, continued, continued
      1. Making the List actually list todos
    15. Test Drive
    16. Implement a way to save the list of todos
    17. So, that’s a UI framework?
    18. SwiftUIcross
    19. Pool Puzzle
    20. BE the Swift Compiler
    21. SwiftUIcross Solution
    22. Pool Puzzle Solution
    23. BE the Swift Compiler Solution
  14. 11. Putting Swiftui into Practice: Circles, Timers, Buttons—Oh My!
    1. What fancy things can be done with a UI framework?
    2. Create a new SwiftUI Xcode project, for iOS
    3. The Executive Timer UI and features
      1. This is what our Executive Timer UI will look like, and do:
      2. And this is what the UI will look like in the various different states:
    4. Creating the basic elements of the app
    5. Pieces of the UI
    6. Setting up the UI of the Executive Timer
    7. Coding the pieces of the UI
      1. 1 Coding the gray circle
      2. 2 Coding the gray circle
      3. 3 Coding the time and buttons
    8. Combining the three elements
    9. The finishing touches
    10. Test Drive
    11. Tabbed views for a clean UI
    12. 1 Create however many different views you need (to live in tabs)
    13. 2 Build a TabView containing your views
    14. The resulting tabs
      1. If you created three views, and a TabView containing them, you might end up here:
    15. Creating a new tabbed ContentView
    16. Creating the tabs and the TabView
      1. 1 Create an empty TimerSettingsView view
      2. 2 Add the second tab to the TabView in the ContentView
      3. We’re basically done! The Executive Timer now has tabs:
    17. Running your new tabbed Executive Timer
    18. SwiftUIcross
    19. SwiftUIcross Solution
  15. 12. Apps, web, and Beyond: Putting It All Together
    1. A journey must end...
    2. A recipe for a welcome screen
    3. Step by step assembly of the welcome screen
    4. Smash Share the state
    5. It’s time for our old friend...
      1. Here’s a type that represents the score of a game:
      2. Here’s some views that use our class:
    6. Building an app with multiple views that share state
      1. Here’s the first view of our hypothetical app:
      2. Here’s the second view of our hypothetical app:
    7. Building a two-view score tracker
      1. Here’s what we’ll need to do:
    8. The ObservableObject
    9. The first view
    10. The second view
    11. The first view, again
    12. A fancy AsyncImage
      1. Like this:
      2. We do this:
    13. Meet Vapor, the Swift web framework
      1. 1 You can make a new Vapor project with the following command:
      2. 2 Change into the Vapor project’s directory with this command:
      3. 3 Then run this to open the new Vapor project in Xcode:
    14. Sending data over the web with Vapor
      1. 1 Create a new closure, with a dynamic parameter:
      2. 2 Add a guard let to unwrap the optional parameter:
      3. 3 Return what you need to return
      4. 4 Visit your new URL
    15. SwiftUIcross
    16. SwiftUIcross
    17. SwiftUIcross Solution
  16. Index

Product information

  • Title: Head First Swift
  • Author(s): Jon Manning, Paris Buttfield-Addison
  • Release date: November 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491922859