Swift in Depth
Tjeerd in 't Veen
  • MEAP began September 2017
  • Publication in Fall 2018 (estimated)
  • ISBN 9781617295188
  • 475 pages (estimated)
  • printed in black & white
Swift is more than just a fun language to build iOS applications with. It features a host of powerful tools that, if you know how to effectively use them, can help create even better apps with clean, crystal-clear code and awesome features. Swift in Depth is designed to help you unlock these tools and quirks and get developing next-gen apps!
Table of Contents detailed table of contents

1. Introducing Swift in depth

1.1. Why Swift?

1.2. Why this book?

1.3. Is this book for you?

1.4. What this book is not

1.5. A big emphasis on practical scenarios

1.6. How to make the most of this book

1.7. Swift version

1.8. Chapter layout

1.8.1. Chapter 2: Modeling data with enums

1.8.2. Chapter 3: Writing cleaner properties

1.8.3. Chapter 4: Making optionals second nature

1.8.4. Chapter 5: Demystifying initializers

1.8.5. Chapter 6: Generics and higher order functions

1.8.6. Chapter 7: Delivering quality Swift code

1.8.7. Chapter 8: Understanding map, flatMap, and compactMap

1.8.8. Chapter 9: Effortless error handling

1.8.9. Chapter 10: Asynchronous error handling with Result

1.8.10. Chapter 11: Putting the pro in Protocol Oriented Programming

1.8.11. Chapter 12: Building data structures with protocols

1.8.12. Chapter 13: Mutability and ownership

1.9. Strap on your seatbelt

2. Modeling data with enums

2.1. Enums instead of subclassing

2.1.1. Forming a model for a workout app

2.1.2. Creating a superclass

2.1.3. The downsides of subclassing

2.1.4. Refactoring a data model with enums

2.1.5. Deciding on subclassing or enums

2.1.6. Exercises

2.2. Enums for polymorphism

2.2.1. Compile-time polymorphism

2.3. Or versus and

2.3.1. Modeling data with a struct

2.3.2. Turning a struct into an enum

2.3.3. Deciding between structs and enums

2.4. Enums are algebraic data types

2.4.1. Algebraic data types

2.4.2. Sum types

2.4.3. Product types

2.4.4. Distributing a sum over an enum

2.4.5. Exercise

2.5. A safer use of strings

2.5.1. Dangers of raw values

2.5.2. Matching on Strings

2.5.3. Exercises

2.5.4. Finishing up

2.6. Summary

2.7. Answers

3. Writing cleaner properties

3.1. Computed properties

3.1.1. Modelling an exercise

3.1.2. Converting functions to computed properties

3.1.3. Rounding up

3.2. Lazy properties

3.2.1. Creating a health plan

3.2.2. When computed properties don’t cut it

3.2.3. Using lazy properties

3.2.4. Making a lazy property robust

3.2.5. Mutable properties and lazy properties

3.2.6. Exercises

3.3. Property observers

3.3.1. Trimming whitespace

3.3.2. Trigger property observers from initializers

3.3.3. Exercises

3.3.4. Finishing up

3.4. Summary

4. Making optionals second nature

4.1. The purpose of optionals

4.2. Clean optional unwrapping

4.2.1. Matching on optionals

4.2.2. Unwrapping techniques

4.2.3. When you’re not interested in a value

4.3. Variable shadowing

4.3.1. Implementing CustomStringConvertible

4.4. When optionals are prohibited

4.4.1. Adding a computed property

4.5. Returning empty strings

4.6. Granular control over optionals

4.6.1. Exercises

4.7. Falling back when an optional is nil

4.8. Simplifying optional enums

4.8.1. Exercise

4.9. Chaining optionals

4.10. Constraining optional booleans

4.10.1. Reducing a boolean to two states

4.10.2. Falling back on true

4.10.3. A boolean with three states

4.10.4. Implementing RawRepresentable

4.10.5. Exercise

4.11. Force unwrapping guidelines

4.11.1. When force unwrapping is "acceptable"

4.11.2. Crashing with style

4.12. Taming Implicitly Unwrapped Optionals

4.12.1. Recognizing IUO’s

4.12.2. When to create IUO’s

4.12.3. Handling retrieved IUO’s

4.12.4. Subtle behavior of IUO’s

4.12.5. Finishing up

4.13. Summary

4.14. Answers

5. Demystifying initializers

5.1. Struct initializer rules

5.1.1. Custom initializers

5.1.2. Struct initializer quirk

5.1.3. Exercises

5.2. Initializers and subclassing

5.2.1. Creating a board game superclass

5.2.2. BoardGame’s initializers

5.2.3. Creating a subclass

5.2.4. Losing convenience initializers

5.2.5. Getting the superclass initializers back

5.2.6. Exercise

5.3. Minimizing class initializers

5.3.1. Convenience overrides

5.3.2. Subclassing a subclass

5.3.3. Exercise

5.4. Required initializers

5.4.1. Factory methods

5.4.2. Protocols

5.4.3. When classes are final

5.4.4. Exercises

5.4.5. Finishing up

5.5. Summary

5.6. Answers

6. Generics and higher-order functions

6.1. Higher-order functions

6.1.1. Creating a function accepting a function

6.1.2. Communicating with a nested function

6.1.3. Filter

6.1.4. Exercise

6.2. Abstracting to a higher-order function

6.2.1. The steps in creating a higher-order function

6.2.2. Exercises

6.3. Making our function generic

6.3.1. Reasoning about generics

6.3.2. Exercises

6.4. Constraining generics

6.4.1. Needing a constrained function

6.4.2. The Equatable and Comparable protocols

6.4.3. Constraining means specializing

6.4.4. Using a generic function on custom types

6.4.5. Constraining versus flexibility

6.5. Multiple constraints

6.5.1. The Hashable protocol

6.5.2. Combining constraints

6.5.3. Exercises

6.6. Creating a generic type

6.6.1. Wanting to combine two Hashable types

6.6.2. Creating a Pair type

6.6.3. Multiple generics

6.6.4. Conforming to Hashable

6.6.5. Hash collisions

6.6.6. Decoupling a hashing function

6.6.7. A general purpose hashing function

6.6.8. Exercise

6.7. Summary

6.8. Answers

7. Delivering quality Swift code

7.1. API documentation

7.1.1. How Quick Help works

7.1.2. Adding callouts to Quick Help

7.1.3. Documentation as HTML with Jazzy


7.2.1. Explain the why

7.2.2. Only explain obscure elements

7.2.3. Code has the truth

7.2.4. Comments are no band-aid to bad names

7.2.5. Zombie code

7.3. Settling on a style

7.3.1. Consistency is key

7.3.2. Enforcing rules with a linter

7.3.3. Installing Swift Lint

7.3.4. Configure SwiftLint

7.3.5. Temporarily disabling SwiftLint rules

7.3.6. Autocorrecting SwiftLint rules

7.3.7. Keeping SwiftLint in sync

7.4. Kill the Managers

7.4.1. The value of managers

7.4.2. Attacking managers

7.4.3. Paving the road for generics

7.5. Naming abstractions

7.5.1. Generic versus specific

7.5.2. Good names don’t change

7.5.3. Generic naming

7.6. Checklist

7.6.1. Finishing up

7.7. Summary

8. Understanding map, flatMap, and compactMap

8.1. Becoming familiar with map

8.1.1. Creating a pipeline with map

8.1.2. Mapping over a dictionary

8.1.3. Exercises

8.2. Mapping over sequences

8.2.1. Exercises

8.3. Mapping over optionals

8.3.1. When to use map

8.3.2. Creating a cover

8.3.3. A shorter map notation

8.3.4. Exercise

8.4. Map is an abstraction

8.5. Grokking flatMap

8.5.1. What are the benefits of flatMap?

8.5.2. When map doesn’t cut it

8.5.3. Fighting the pyramid of doom

8.5.4. FlatMapping over an optional

8.5.5. Closing thoughts

8.6. flatMapping over collections

8.6.1. flatMapping over strings

8.6.2. Using compactMap

8.6.3. Nesting or chaining

8.6.4. Exercises

8.7. Summary

8.8. Answers

9. Effortless error handling

9.1. Errors in Swift

9.1.1. The Error protocol

9.1.2. Throwing errors

9.1.3. Swift doesn’t reveal errors

9.1.4. Keeping the environment in a predictable state

9.1.5. Exercises

9.2. Error propagation and catching

9.2.1. Propagating errors

9.2.2. Adding technical details for troubleshooting

9.2.3. Centralizing error handling

9.2.4. Exercises

9.3. Delivering pleasant API’s

9.3.1. Capturing validity within a type

9.3.2. Try?

9.3.3. Try!

9.3.4. Using optionals

9.3.5. Exercises

9.4. Summary

9.5. Answers

10. Asynchronous error handling with Result

10.1. Why use the Result type?

10.1.1. Result is like Optional, with a twist

10.1.2. Understanding the benefits of Result

10.1.3. Creating an API using Result

10.1.4. Bridging from Cocoa Touch to Result

10.2. Propagating Result

10.2.1. Typealiasing for convenience

10.2.2. The search function

10.3. Transforming values inside Result

10.3.1. Exercise

10.3.2. flatMapping over Result

10.3.3. Exercises

10.4. Mixing Result with throwing functions

10.4.1. From throwing to a Result type

10.4.2. Converting a throwing function inside flatMap

10.4.3. Weaving errors through a pipeline

10.4.4. Finishing up

10.4.5. Exercises

10.5. Multiple errors inside of Result

10.5.1. Introducing AnyError

10.5.2. Final thoughts

10.6. Summary

10.7. Answers

11. Putting the pro in Protocol Oriented Programming

12. Iterators, Sequence, and Collection

13. Patterns with Protocols

About the Technology

Apple's Swift is the standard language for iOS and Mac development. It's is a modern marvel—easy to pick up, comfortable to use, and loaded with features. After you master the basics, there's a rich, deep world of Swift skills for delivering cleaner, faster code. This book distills the hard-won experience of seasoned Swift developer Tjeerd in 't Veen into a collection of high-value, hard-to-find techniques that will help set you and your Swift applications apart.

About the book

Swift in Depth builds on your core Swift language skills, introducing you to powerful techniques like using higher-order functions, generics, efficient error handling, and protocol-oriented programming. You'll explore each new concept through concrete examples, enlightened explanations, and engaging exercises. Some of this stuff is difficult, so you'll really appreciate the careful, crystal clear writing and precise illustrations. When you're finished, you'll be a more productive Swift developer, and a happier Swift developer, as well!

What's inside

  • Adding advanced Swift features to your code
  • Writing reusable code with generics
  • Iterators, sequences, and collections
  • Protocol oriented programming

About the reader

Readers should have some prior experience with the basics of Swift.

About the author

Tjeerd in 't Veen is a senior software engineer and architect who builds iOS apps. Having worked together with many developers, he currently helps creating clean, scalable architectures in the mobile division of a large international banking firm.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook + liveBook
MEAP eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks