The Swift™ Developer’s Cookbook

Book description

In The Swift Developer's Cookbook, renowned author Erica Sadun joins powerful strategies with ready-to-use Swift code for solving everyday development challenges.

As in all of Sadun's programming best-sellers, The Swift Developer's Cookbook translates modern best practices into dozens of well-tested, easy-to-apply solutions. This book's code examples were created in response to real-world questions from working developers to reflect Swift's newest capabilities and best practices. Each chapter groups related tasks together. You can jump straight to your solution without having to identify the right class or framework first.

Sadun covers key Swift development concepts, shows you how to write robust and efficient code, and helps you avoid common pitfalls other developers struggle with. She offers expert strategies for working with this immensely powerful language, taking into account Swift's rapid evolution and its migration tools.

Whether you're moving to modern Swift from Objective-C, from older versions of the Swift language, or from the world of non-Apple languages, this guide will help you master both the "how" and "why" of effective Swift development. Industry recruiters are scrambling to find Swift developers who can solve real problems and produce effective working code. Get this book, and you’ll be ready.

Coverage includes

  • Writing effective Swift code that communicates clearly and coherently to the compiler, your team, and to "future you," who will be maintaining this code

  • Using Xcode to handle changes in Swift's language constructs as the language evolves

  • Building feedback, documentation, and output to meet your development and debugging needs

  • Making the most of optionals and their supporting constructs

  • Using closures to encapsulate state and functionality and treat actions as variables for later execution

  • Leveraging control flow with innovative Swift-specific statements

  • Working with all Swift types: classes, enumerations, and structures

  • Using generics and protocols to build robust code that expands functionality beyond single types

  • Making the most of the powerful Swift error system

  • Working with innovative features such as array indexing, general subscripting, statement labels, custom operators, and more

  • Table of contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Contents
    5. Preface
      1. How This Book Is Organized
      2. About the Sample Code
        1. Contribute!
        2. Getting GitHub
      3. Contacting the Author
    6. Acknowledgments
    7. About the Author
    8. Editor’s Note: We Want to Hear from You!
    9. Reader Services
    10. 1. Welcome to Modern Swift
      1. Migrating Code
        1. How to Migrate
        2. Migration Lessons
      2. Using Swift
        1. Compiled Applications
        2. Frameworks and Libraries
        3. Scripting
        4. REPL
        5. Playgrounds
        6. Other Swift
      3. Learning Swift
      4. Wrap-up
    11. 2. Printing and Mirroring
      1. Basic Printing
        1. Printing Multiple Items
        2. Adding Item Separators
        3. String Interpolation
        4. Controlling Line Feeds
      2. Recipe: Printing to Custom Destinations
        1. Printing to Strings
        2. Printing to Custom Output Streams
      3. Recipe: Printing and String Formats
        1. Swift and Format Specifiers
        2. Format Limitations
        3. Conditional Compilation
      4. Debug Printing
        1. Custom Stream Output
      5. Recipe: Postfix Printing
      6. Quick Looks
        1. Quick Look for Custom Types
        2. Quick Looks for Custom Types in Playgrounds
        3. Playground Quick Looks
        4. Creating Custom Quick Looks for Playgrounds
        5. Built-in Quick Look Types
        6. Third-Party Rendering
      7. Using Dump Functions
      8. Building Custom Mirrors
        1. Recursive Reflection
        2. Building Basic Mirror Descriptions Using Protocol Conformance
      9. Adding Header Docs
        1. Building Header Documentation
        2. Markdown Support
        3. Keywords
        4. Special Swift Concerns
        5. Adding Images to Header Docs
      10. Wrap-up
    12. 3. Optionals?!
      1. Optionals 101
      2. Unwrapping Optionals
        1. Forced Unwrapping
        2. Conditional Binding
        3. Conditional Binding and Type Casts
        4. Binding Cascades
        5. guard Statements
        6. Implicitly Unwrapped Optionals
        7. Guarding Failable Initializers
        8. Optionals and Sentinel Values
        9. Coalescing
        10. Optional Assignment
        11. Optional Patterns
      3. Optional Chaining
        1. Selector Testing and Optional Chaining
        2. Subscripts
      4. Optional Mapping
        1. Maps and Chaining
        2. Filtering nil Values with flatMap
      5. Unmanaged Wrappers
      6. Wrap-up
    13. 4. Closures and Functions
      1. Building a Function
        1. Parameter Names
        2. Label Conventions
        3. Naming Methods and Functions
        4. External and Local Parameters
        5. Defaults
        6. Constant and Variable Parameters
        7. Modifying Parameters
      2. Closures and Functions
        1. Function Types
        2. Using Tuple Arguments
        3. Using Shorthand Argument Names
        4. Inferred Closure Types
        5. Parameter Type Inference
        6. Declaring Type Aliases
        7. Nested Functions
      3. Tuples
        1. From Tuples to Structs
        2. Tuple Return Types
      4. Variadic Parameters
      5. Capturing Values
      6. Autoclosures
        1. Adding Default Closures
      7. Currying
        1. Why Curry?
        2. Building Libraries
        3. Partial Application
        4. Currying Costs
        5. Currying and Callbacks
        6. Practical Currying
      8. Passing Closures as Function Parameters
      9. Wrap-up
    14. 5. Generics and Protocols
      1. Expanding to Generics
        1. Protocols
        2. Tokens
        3. Type Constraints
      2. Adopting Protocols
        1. Declaring Protocols
        2. Member Declarations
        3. Building a Basic Protocol
        4. Adding Protocol Default Implementations
        5. Optional Protocol Requirements
        6. Swift-Native Optional Protocol Requirements
      3. Building Generic Types
        1. Type Parameters
      4. Generic Requirements
        1. Conformance Requirements
        2. Recipe: Same-Type Requirements
        3. Generic Beautification
        4. Legal Tokens
        5. Matching Aliases
        6. Protocol Alias Defaults
        7. Collating Associated Types
      5. Extending Generic Types
      6. Using Protocols as Types
        1. Protocol-Based Collections
        2. Self Requirements
        3. Protocol Objects and Self Requirements
      7. Leveraging Protocols
      8. Wrap-up
    15. 6. Errors
      1. Failing Hard
        1. Fatal Errors
        2. Assertions
        3. Preconditions
        4. Aborting and Exiting
      2. Failing Gracefully
        1. The ErrorType Protocol
        2. Choosing Between Optionals and Error Handling
      3. Swift Error Rules
        1. Rule 1: Move Away from nil Sentinels
        2. Rule 2: Use throw to Raise Errors
        3. Rule 3: Use Error Types with Visible Access
        4. Rule 4: Mark All Error-Participating Methods with throws
        5. Rule 5: Use rethrows Consistently
        6. Rule 6: Consume Errors Where They Matter
        7. Rule 7: Terminate Threaded Error Chains
      4. Building Errors
        1. Good Errors
        2. Naming Your Errors
      5. Adding String Descriptions
        1. Adding Reasons
        2. Simplifying Output
        3. Extending String
        4. Type-Specific Errors
      6. Retrieving Context
        1. Contextualizing Strings
        2. Contextualizing Throwing Types
        3. Simplifying Contexts
      7. Calling Throwing Functions
        1. Using try
        2. Error Propagation
        3. Using try!
        4. Using try?
      8. Implementing Alternatives to try?
        1. Guarding Results
        2. Building a Printing Version of try?
      9. Working with guard and defer
      10. Wrap-up
    16. 7. Types
      1. Language Concepts
        1. Reference and Value Types
        2. Copy-and-Write-Back
        3. Algebraic Data Types
        4. Other Terminology
      2. Enumerations
        1. Basic Enumerations
        2. Using Hash Values to Support Ranges
        3. Raw Value Enumerations
        4. Raw Value Members and Sequences
        5. Associated Values
        6. Indirect Values
      3. Switches
        1. Branching
        2. Breaks
        3. Fallthroughs
        4. Complex Cases
        5. Tuples
        6. Pattern Matching with Value Bindings
        7. where Clauses
        8. Unwrapping Optional Enumerations
      4. Embedding Values by Type
      5. Option Sets
        1. Revisiting NS_OPTIONS
        2. Building Enumerations
        3. Building Option Sets
        4. Viewing Options
      6. Classes
        1. Optimization
        2. Initializers
        3. Initialization Steps
        4. Designated and Convenience Initializers
        5. Initialization Rules
        6. Building Convenience Initializers
        7. Failable and Throwing Initializers
        8. Deinitializers
      7. Property Observers
        1. Getters/Setters and Access-Level Modifiers
      8. Extensions and Overrides
      9. Lazy Evaluation
        1. Lazy Sequences
        2. Lazy Properties
      10. Wrap-up
    17. 8. Miscellany
      1. Statement Labels
      2. Custom Operators
        1. Declaring Operators
        2. Conforming with Operators
        3. Evaluating Operator Trade-offs
      3. Array Indexing
        1. Multi-indexed Array Access
        2. Wrapped Indices
        3. Array Slices
      4. General Subscripting
        1. Parameter-less Subscripting
      5. String Utilities
        1. Repeat Initializers
        2. Strings and Radix
        3. String Ranges
        4. String Splitting
        5. String Subscripts
        6. Foundation Interoperability
        7. Joining and Extending
      6. Permutation Generator
      7. Wrap-up
    18. Index
    19. Code Snippets

    Product information

    • Title: The Swift™ Developer’s Cookbook
    • Author(s): Erica Sadun
    • Release date: December 2015
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780134395364