O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beginning Swift

Book Description

Master the fundamentals of programming in Swift 4

About This Book
  • Covers theory and practice in equal parts
  • Teaches you how to correctly structure and architect software using Swift
  • Uses real-world examples to connect the theory to a professional setting
  • Imparts expertise in the core Swift standard library
Who This Book Is For

If you are seeking fundamental Swift programming skills, in preparation for learning to develop native applications for iOS or macOS, this book is the best for you. You don't need to have any prior Swift knowledge; however, object-oriented programming experience is desired.

What You Will Learn
  • Explore the fundamental Swift programming concepts, language structure, and the Swift programming syntax
  • Learn how Swift compares to other computer languages and how to transform your thinking to leverage new concepts such as optionals and protocols
  • Master how to use key language elements, such as strings and collections
  • Grasp how Swift supports modern application development using advanced features, such as built-in Unicode support and higher-order functions.
In Detail

Take your first foray into programming for Apple devices with Swift.

Swift is fundamentally different from Objective-C, as it is a protocol-oriented language. While you can still write normal object-oriented code in Swift, it requires a new way of thinking to take advantage of its powerful features and a solid understanding of the basics to become productive.

This course helps you develop client-side and server-side applications, as well as web services using Swift. We'll begin with exploring the fundamental Swift programming concepts, language structure, and the Swift programming syntax. Then, we'll learn to create original custom operators with Swift operators, branching, and loops.

Moving on, we'll learn how to run application codes and compile errors. Having made progress with it, we'll see how Swift compares to other computer languages and how to transform your thinking. Then, master the usage of key language elements, such as strings and collections. Finally, grasp how Swift supports modern application development using advanced features, such as built-in Unicode support and higher-order functions.

This is an introductory course to the Swift programming language with Xcode.After completing this course, students will be well-prepared to begin developing native end-user applications for iOS or macOS, or to develop server-side (back-end) application and web services using Swift on Linux.

Style and approach

This is an introductory course to the Swift programming language with Xcode. The course does not expect you to have any previous Swift knowledge or experience. The course covers ample amount of exercises so that students learn the basics hands-on.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Beginning Swift
    1. Table of Contents
    2. Beginning Swift
      1. Why Subscribe?
      2. PacktPub.com
    3. Contributors
      1. About the Authors
      2. Packt is Searching for Authors Like You
    4. Preface
      1. What This Book Covers
      2. What You Need for This Book
      3. Who This Book is for
      4. Conventions
      5. Reader Feedback
      6. Customer Support
        1. Downloading the Example Code
        2. Errata
        3. Piracy
        4. Questions
    5. 1. Swift Basics
      1. Lesson objectives
      2. Swift Program Structure
        1. Hello, World!
      3. Swift Variables and Constants
        1. Declaring Swift Variables
        2. Variables Versus Constants
        3. Type Inference
        4. Variable Naming
          1. Working with Variables
        5. Tuples
          1. Creating a Tuple
        6. Optionals
          1. Declaring an Optional
          2. Working with Optionals
          3. Optional nil Values
          4. Accessing Optional Values
          5. Force Unwrapping an Optional
          6. Conditionally Unwrapping Optionals
            1. Using Optionals
          7. The Swift guard Statement
        7. Activity B: Variable Summary
      4. Swift Data Types
        1. Numeric Data Types
          1. Int on 64-Bit Versus 32-Bit Platforms
          2. Built-In Numeric Data Types
          3. Choosing the Appropriate Numeric Data Type
          4. Declaring and Assigning Integer Variables
          5. Declaring and Assigning Floating Point Numbers
          6. Numeric Literal Grouping
          7. Numeric Type Conversions
            1. Using Numeric Types
        2. Boolean
        3. Character
          1. Assigning a Character
          2. Constructing a Character Literal
        4. String
          1. Instantiating a String
          2. String Concatenation
          3. Extracting Characters
          4. String Length
        5. Activity C: Data Type Summary
      5. Enums
        1. Basic Enum Syntax
        2. Enum with Raw Values
        3. Activity D: Using Swift Enums
      6. Summary
    6. 2. Swift Operators and Control Flow
      1. Lesson objectives
      2. Swift Operators
        1. Assignment Operator
        2. Arithmetic Operators
          1. Standard Arithmetic Operators
          2. Remainder Operator
          3. Unary minus Operator
          4. Compound Assignment Operators
        3. Comparison Operators
          1. Equality
          2. Inequality
          3. Comparison between Two Values
        4. Ternary Conditional Operator
        5. Logical Operators
        6. Bitwise Operators
        7. Nil-Coalescing Operator
        8. Range Operators
          1. Closed Range Operator
          2. Half-Open Range Operator
          3. One-Sided Range Operator
        9. Activity A: Operators
      3. Branching
        1. The if Statement
          1. Condition Lists
          2. Optional Unwrapping with if
        2. The switch Statement
          1. switch Statement Rules
          2. The break Keyword
          3. The fallthrough Keyword
          4. Matching Non-Scalar Values
          5. Multiple Patterns in a Single Case
          6. Using the where Statement within case
          7. Evaluating Optionals with a switch Statement
        3. Activity B: Converting Code from if to switch
      4. Loops
        1. The for…in Statement
          1. Iterating over Objects
          2. Iterating over Array Objects with index
          3. The for Loop where Clause
          4. The break Control Transfer Statement
          5. The continue Control Transfer Statement
        2. The while Loop
          1. The repeat…while Loop
        3. Activity C: Implementing Loops
      5. Summary
    7. 3. Functions, Classes, and Structs
      1. Lesson Objectives
      2. Functions
        1. Defining a Function
        2. Argument Labels
        3. Excluding Argument Labels
        4. Parameter Default Values
        5. Activity A: Implementing a Function
        6. Returning Values from Functions
        7. Using @discardableResult
        8. Function Attributes
        9. Variadic Parameters
        10. inout Parameters
        11. Recursion
        12. Functions as Parameters
        13. Closures
          1. Creating a Function to Receive Content from an Asynchronous Web Service Call
      3. Error Handling
        1. The do…catch Statement
        2. Multiple catch Blocks
        3. Using do without catch
        4. The guard Statement
        5. Activity B: Exception Handling
      4. Object-Oriented Features
        1. Object-Oriented Principles
        2. Classes Versus Structs
          1. Illustration
        3. Defining Classes and Structures
        4. Activity C: Creating a Customer Struct and Class
      5. Summary
      6. Challenge
    8. 4. Collections
      1. Lesson Objectives
      2. Arrays
        1. Working with Arrays
        2. Index
          1. Utilizing Common Operations with Index
        3. ArraySlice
          1. Creating Slices
          2. Creating Slices Using Range Operators
        4. Activity A: Working with Arrays
      3. Sets
        1. Working with Sets
        2. Combining Sets
        3. Comparing Sets
        4. Activity B: Removing Duplicates from a Sequence
      4. Dictionaries
        1. Working with Dictionaries
        2. Activity C: Using Dictionaries
      5. Summary
    9. 5. Strings
      1. Lesson Objectives
      2. String Fundamentals
        1. Character
        2. Collection
        3. Index
          1. Working with String Index
        4. Debugging
        5. Activity A: All Indices of a Character
      3. Using Strings
        1. Creating Strings
        2. Common Operations
          1. Implementing Extra Text Operations on a String
        3. Activity B-1: All Ranges of a Substring
        4. Activity B-2: Counting Words, Sentences, and Paragraphs
      4. Substring
        1. Creating Substrings
          1. Parsing Strings
        2. Converting NSRange to Range
        3. Activity C: CamelCase
      5. Summary
    10. 6. Functional Programming and Lazy Operations
      1. Lesson Objectives
      2. Function Type
      3. Functional Methods
        1. filter
          1. Using the filter Method
        2. map
          1. Using the map Function
        3. flatMap
          1. Using the flatMap Function
        4. reduce
          1. Using the reduce Function
        5. Activity A: Implementing Functional Programming
      4. Lazy Operations
        1. Lazy Sequences
        2. Sequence Internals
        3. Creating Lazy Operations
          1. sequence(first:next:)
          2. sequence(state:next:)
        4. Activity B: Implementing a Lazy Version of a Method
      5. Swifty Code
        1. Naming
        2. Organizing Code
        3. Miscellaneous
          1. Writing Swifty Code
      6. Summary
      7. Further Study
      8. Challenge
    11. Index