Swift 4 Programming Cookbook

Book description

Over 50 recipes to help you quickly and efficiently build applications with Swift 4 and Xcode 9

About This Book

  • Write robust and efficient code and avoid common pitfalls using Swift 4
  • Get a comprehensive coverage of the tools and techniques needed to create multi-platform apps with Swift 4
  • Packed with easy-to-follow recipes, this book will help you develop code using the latest version of Swift

Who This Book Is For

If you are looking for a book to help you learn about the diverse features offered by Swift 4 along with tips and tricks to efficiently code and build applications, then this book is for you. Basic knowledge of Swift or general programming concepts will be beneficial.

What You Will Learn

  • Explore basic to advanced concepts in Swift 4 Programming
  • Unleash advanced features of Apple's Xcode 9 IDE and Swift Playgrounds
  • Learn about the conditional statements, loops, and how to handle errors in Swift
  • Define flexible classes and structs using Generics, and learn about the advanced operators, and create custom operators
  • Explore functionalities outside of the standard libraries of Swift
  • Import your own custom functionality into Swift Playgrounds
  • Run Swift on Linux and investigate server-side programming with the server side framework Vapor

In Detail

Swift 4 is an exciting, multi-platform, general-purpose programming language. Being open source, modern and easy to use has made Swift one of the fastest growing programming languages. If you interested in exploring it, then this book is what you need.

The book begins with an introduction to the basic building blocks of Swift 4, its syntax and the functionalities of Swift constructs. Then, introduces you to Apple's Xcode 9 IDE and Swift Playgrounds, which provide an ideal platform to write, execute, and debug the codes thus initiating your development process. Next, you'll learn to bundle variables into tuples, set order to your data with an array, store key-value pairs with dictionaries and you'll learn how to use the property observers. Later, explore the decision-making and control structures in Swift and learn how to handle errors in Swift 4.

Then you'll, examine the advanced features of Swift, generics and operators, and then explore the functionalities outside of the standard library, provided by frameworks such as Foundation and UIKit. Also, you'll explore advanced features of Swift Playgrounds. At the end of the book, you'll learn server-side programming aspect of Swift 4 and see how to run Swift on Linux and then investigate Vapor, one of the most popular server-side frameworks for Swift.

Style and approach

Each recipe addresses a specific problem, with a detailed discussion that explains the solution and offers insight into how it works.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Conventions
    6. Reader feedback
    7. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Swift Building Blocks
    1. Introduction
    2. Your first Swift program
      1. Getting ready
        1. Downloading Xcode
      2. How to do it...
      3. There's more...
      4. See also
    3. Strings, Ints, Floats, and Bools
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Optionals, unwrap, and force unwrap
      1. Getting started
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Functions
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Default parameter values
        2. Parameter overloading
      4. See also
    6. Object classes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Structs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Enumerations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Methods and computed variables
        2. Associated values
      5. See also
    9. Closures
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Protocols
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  3. Building on the Building Blocks
    1. Introduction
    2. Bundling variables into tuples
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Ordering your data with arrays
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Containing your data with sets
      1. How to do it...
      2. How it works...
        1. Union
        2. Intersection
        3. Symmetric difference
        4. Subtracting
      3. There's more...
      4. See also
    5. Storing key-value pairs with Dictionaries
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Subscripts for custom types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Changing your name with typealias
      1. How to do it...
      2. There's more...
      3. See also
    8. Getting property changing notifications using property observers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Controlling access with access control
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Extending functionality with extensions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  4. Data Wrangling with Swift Control Flow
    1. Introduction
    2. Making decisions with if/else
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Conditional unwrap
        2. Chain unwrapping Optionals
        3. Enum with associated values
      5. See also
    3. Switch it up
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. For the love of loops
      1. How to do it...
      2. How it works...
      3. See also
    5. While loops
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Try, throw, do and, catch - Swift error handling
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Checking up front with guard
      1. How to do it...
      2. How it works...
      3. See also
    8. Doing it later with defer
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Bailing out with fatalError and precondition
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  5. Generics, Operators, and Nested Types
    1. Introduction
    2. Using generics with types
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    3. Using generics with functions
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Using generics with protocols
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. Advanced operators
      1. Getting ready
      2. How to do it...
      3. See also
    6. Option set
      1. How to do it...
      2. How it works...
      3. See also
    7. Create custom operators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Nested types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  6. Beyond the Standard Library
    1. Introduction
    2. Foundation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Networking
      1. How to do it...
      2. How it works...
      3. See also
    4. JSON
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
    5. XML
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Cocoa Touch
      1. Getting ready
      2. How to do it...
      3. There's more...
      4. See also
  7. Swift Playgrounds
    1. Introduction
    2. Using Swift Playgrounds for UI
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Import Resources into Playgrounds
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Import Code into Playgrounds
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Multi-Page Playgrounds
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  8. Server-Side Swift
    1. Introduction
    2. Swift on Ubuntu
      1. Getting started
      2. How to do it...
      3. There's more...
    3. Building a REST API using Vapor
      1. Getting started
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Persistence with Vapor using Postgres
      1. Getting started
      2. How to do it...
      3. There's more...
      4. See also
    5. Hosting your Vapor app on Heroku
      1. Getting started
      2. How to do it...
      3. See also
  9. Performance and Responsiveness in Swift
    1. Introduction
    2. Value and reference semantics
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Dispatch Queues
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Concurrent queues and dispatch groups
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Operations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also

Product information

  • Title: Swift 4 Programming Cookbook
  • Author(s): Keith Moon
  • Release date: September 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781786460899