Swift in Depth
Tjeerd in 't Veen
  • MEAP began September 2017
  • Publication in October 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: Effortless error handling

1.8.6. Chapter 7: Generics

1.8.7. Chapter 8: Putting the pro in Protocol-Oriented Programming

1.8.8. Chapter 9: Iterators, Sequences, and Collections

1.8.9. Chapter 10: Protocol extensions

1.8.10. Chapter 11: Understanding map, flatMap, and compactMap

1.8.11. Chapter 12: Asynchronous error handling with Result

1.8.12. Chapter 13: Patterns with Protocols

1.8.13. Chapter 14: Delivering quality Swift code

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.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 learning 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. Closing thoughts

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.6.2. Falling back when an optional is nil

4.6.3. Simplifying optional enums

4.6.4. Exercise

4.7. Chaining optionals

4.8. Constraining optional booleans

4.8.1. Reducing a boolean to two states

4.8.2. Falling back on true

4.8.3. A boolean with three states

4.8.4. Implementing RawRepresentable

4.8.5. Exercise

4.9. Force unwrapping guidelines

4.9.1. When force unwrapping is "acceptable"

4.9.2. Crashing with style

4.10. Taming Implicitly Unwrapped Optionals

4.10.1. Recognizing IUO’s

4.10.2. IUO’s in practice

4.11. Exercise

4.11.1. Closing thoughts

4.12. Summary

4.13. 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. Closing thoughts

5.5. Summary

5.6. Answers

6. Effortless error handling

6.1. Errors in Swift

6.1.1. The Error protocol

6.1.2. Throwing errors

6.1.3. Swift doesn’t reveal errors

6.1.4. Keeping the environment in a predictable state

6.1.5. Exercises

6.2. Error propagation and catching

6.2.1. Propagating errors

6.2.2. Adding technical details for troubleshooting

6.2.3. Centralizing error handling

6.2.4. Exercises

6.3. Delivering pleasant API’s

6.3.1. Capturing validity within a type

6.3.2. Try?

6.3.3. Try!

6.3.4. Using optionals

6.3.5. Exercises

6.4. Summary

6.5. Answers

7. Generics

7.1. The benefits of generics

7.1.1. Creating a generic function

7.1.2. Reasoning about generics

7.1.3. Exercises

7.2. Constraining generics

7.2.1. Needing a constrained function

7.2.2. The Equatable and Comparable protocols

7.2.3. Constraining means specializing

7.2.4. Implementing Comparable

7.2.5. Constraining versus flexibility

7.2.6. Exercise

7.3. Multiple constraints

7.3.1. The Hashable protocol

7.3.2. Combining constraints

7.3.3. Exercise

7.4. Creating a generic type

7.4.1. Wanting to combine two Hashable types

7.4.2. Creating a Pair type

7.4.3. Multiple generics

7.4.4. Conforming to Hashable

7.4.5. Exercise

7.5. Generics and subtypes

7.5.1. Subtyping and invariance

7.5.2. Invariance in Swift

7.5.3. Swift’s generic types get special privileges

7.5.4. Summary

7.6. Answers

8. Putting the pro in Protocol-Oriented Programming

8.1. Runtime versus compile-time

8.1.1. Creating a protocol

8.1.2. Generics versus protocols

8.1.3. A trade-off with generics

8.1.4. Moving to runtime

8.1.5. Choosing between compile-time and runtime

8.1.6. When a generic is the better choice

8.1.7. Exercises

8.2. The why of associated types

8.2.1. The deal with generic protocols

8.2.2. Defining associated types

8.2.3. PATs in the standard library

8.2.4. Other uses for associated types

8.2.5. Exercises

8.3. Passing protocols with associated types

8.3.1. Where clauses with associated types

8.3.2. Types constraining associated types

8.3.3. Cleaning up our API with Protocol inheritance

8.3.4. Closing thoughts

8.3.5. Exercises

8.4. Summary

8.5. Answers

9. Iterators, Sequences, and Collections

10. Protocol extensions

11. Understanding map, flatMap, and compactMap

11.1. Becoming familiar with map

11.1.1. Creating a pipeline with map

11.1.2. Mapping over a dictionary

11.1.3. Exercises

11.2. Mapping over sequences

11.2.1. Exercises

11.3. Mapping over optionals

11.3.1. When to use map on optionals

11.3.2. Creating a cover

11.3.3. A shorter map notation

11.3.4. Exercise

11.4. Map is an abstraction

11.5. Grokking flatMap

11.5.1. What are the benefits of flatMap?

11.5.2. When map doesn’t cut it

11.5.3. Fighting the pyramid of doom

11.5.4. FlatMapping over an optional

11.5.5. Closing thoughts

11.6. flatMapping over collections

11.6.1. flatMapping over strings

11.6.2. Using compactMap

11.6.3. Nesting or chaining

11.6.4. Exercises

11.6.5. Summary

11.7. Answers

12. Asynchronous error handling with Result

12.1. Why use the Result type?

12.1.1. Result is like Optional, with a twist

12.1.2. Understanding the benefits of Result

12.1.3. Creating an API using Result

12.1.4. Bridging from Cocoa Touch to Result

12.2. Propagating Result

12.2.1. Typealiasing for convenience

12.2.2. The search function

12.3. Transforming values inside Result

12.3.1. Exercise

12.3.2. flatMapping over Result

12.3.3. Exercises

12.4. Mixing Result with throwing functions

12.4.1. From throwing to a Result type

12.4.2. Converting a throwing function inside flatMap

12.4.3. Weaving errors through a pipeline

12.4.4. Finishing up

12.4.5. Exercises

12.5. Multiple errors inside of Result

12.5.1. Introducing AnyError

12.5.2. Closing thoughts

12.6. Summary

12.7. Answers

13. Patterns with Protocols

14. Delivering quality Swift code

14.1. API documentation

14.1.1. How Quick Help works

14.1.2. Adding callouts to Quick Help

14.1.3. Documentation as HTML with Jazzy

14.2. Comments

14.2.1. Explain the why

14.2.2. Only explain obscure elements

14.2.3. Code has the truth

14.2.4. Comments are no band-aid to bad names

14.2.5. Zombie code

14.3. Settling on a style

14.3.1. Consistency is key

14.3.2. Enforcing rules with a linter

14.3.3. Installing Swift Lint

14.3.4. Configure SwiftLint

14.3.5. Temporarily disabling SwiftLint rules

14.3.6. Autocorrecting SwiftLint rules

14.3.7. Keeping SwiftLint in sync

14.4. Kill the Managers

14.4.1. The value of managers

14.4.2. Attacking managers

14.4.3. Paving the road for generics

14.5. Naming abstractions

14.5.1. Generic versus specific

14.5.2. Good names don’t change

14.5.3. Generic naming

14.6. Checklist

14.6.1. Finishing up

14.7. Summary

15. Where to Swift from here

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.
buy
MEAP combo $49.99 pBook + eBook + liveBook
MEAP eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks