You are previewing Learning Swift - Second Edition.
O'Reilly logo
Learning Swift - Second Edition

Book Description

Develop the skills required to create compelling, maintainable, and robust iOS and OS X apps with Swift

About This Book

  • Write expressive, understandable, and maintainable Swift 2 code with this hands-on tutorial

  • Unveil the complex underpinnings of Swift to turn your app ideas into reality

  • This book is packed with real-life examples to help you implement concepts as you learn

  • Who This Book Is For

    If you are looking to build iOS or OS X apps using the most modern technology, this book is ideal for you. You will find this book especially useful if you are new to programming or if you are yet to develop for iOS or OS X. No prior programming exposure is required.

    What You Will Learn

  • Form a solid understanding of the Swift 2 language

  • Get to know the practical aspects of how a computer program actually works

  • Understand the paradigms used by Apple’s frameworks so you are not intimidated by them

  • Utilize the vast resources written in Objective-C to better inform your Swift programming

  • Develop a basic portfolio of Swift code by learning the critical concepts

  • Experience both object-oriented and functional programming

  • Get to know the new coding techniques made available by Swift 2

  • Discover resources to ensure you never stop becoming a better developer

  • In Detail

    Swift is Apple’s new programming language and the future of iOS and OS X app development. It is a high-performance language that feels like a modern scripting language. On the surface, Swift is easy to jump into, but it has complex underpinnings that are critical to becoming proficient at turning an idea into reality.

    This book is an approachable, step-by-step introduction into programming with Swift for everyone. It begins by giving you an overview of the key features through practical examples and progresses to more advanced topics that help differentiate the proficient developers from the mediocre ones. It covers important concepts such as Variables, Optionals, Closures, Generics, and Memory Management. Mixed in with those concepts, it also helps you learn the art of programming such as maintainability, useful design patterns, and resources to further your knowledge. This all culminates in writing a basic iOS app that will get you well on your way to turning your own app ideas into reality.

    Style and approach

    This is an approachable, step-by-step guide to programming in Swift 2. Each topic is separated into compressible sections that are full of practical examples and easy-to-understand explanations. Each section builds on the previous topics so you can develop a proficient and comprehensive understanding of app development in Swift 2.

    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 code file.

    Table of Contents

    1. Learning Swift Second Edition
      1. Table of Contents
      2. Learning Swift Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. 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. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Introducing Swift
        1. Defining our goals for this book
        2. Setting up the development environment
        3. Running our first swift code
        4. Understanding playgrounds
        5. Learning with this book
        6. Summary
      9. 2. Building Blocks – Variables, Collections, and Flow Control
        1. Core Swift types
          1. Constants and variables
          2. Containers
            1. Tuples
            2. Arrays
            3. Dictionaries
        2. Swift's type system
        3. Printing to the console
        4. Control flow
          1. Conditionals
          2. Switches
          3. Loops
        5. Functions
          1. Basic functions
          2. Parameterized functions
          3. Functions that return values
          4. Functions with default arguments
          5. Guard statement
        6. Bringing it all together
        7. Summary
      10. 3. One Piece at a Time – Types, Scopes, and Projects
        1. Structs
          1. Types versus instances
          2. Properties
          3. Member and static methods
          4. Computed properties
          5. Reacting to property changes
          6. Subscripts
          7. Custom initialization
        2. Classes
          1. Inheriting from another class
          2. Initialization
            1. Overriding initializer
            2. Required initializer
            3. Designated and convenience initializers
          3. Overriding methods and computed properties
            1. Methods
            2. Computed properties
          4. Casting
            1. Upcasting
            2. Downcasting
        3. Enumerations
          1. Basic declaration
          2. Testing enumeration values
          3. Raw values
          4. Associated values
          5. Methods and properties
        4. Projects
          1. Setting up a command-line Xcode project
          2. Creating and using an external file
          3. Interfacing with code from other files
          4. File organization and navigation
        5. Extensions
        6. Scope
          1. How scope is defined
          2. Nested types
        7. Access control
        8. Summary
      11. 4. To Be or Not To Be – Optionals
        1. Defining an optional
        2. Unwrapping an optional
          1. Optional binding
          2. Forced unwrapping
          3. Nil coalescing
        3. Optional chaining
        4. Implicitly unwrapped optionals
        5. Debugging optionals
        6. The underlying implementation
        7. Summary
      12. 5. A Modern Paradigm – Closures and Functional Programming
        1. Functional programming philosophy
          1. State and side effects
          2. Declarative versus imperative code
        2. Closures
          1. Closures as variables
          2. Closures as parameters
          3. Syntactic sugar
        3. Building blocks of functional programming in Swift
          1. Filter
          2. Reduce
          3. Map
          4. Sort
          5. How these affect the state and nature of code
        4. Lazy evaluation
        5. Example
        6. Summary
      13. 6. Make Swift Work For You – Protocols and Generics
        1. Protocols
          1. Defining a protocol
          2. Implementing a protocol
          3. Using type aliases
        2. Generics
          1. Generic function
          2. Generic type
          3. Type constraints
            1. Protocol constraints
            2. Where clauses for protocols
            3. Where clauses for equality
        3. Extending generics
          1. Adding methods to all forms of a generic
          2. Adding methods to only certain instances of a generic
          3. Extending protocols
        4. Putting protocols and generics to use
          1. Generators
          2. Sequences
          3. Product of Fibonacci numbers under 50
        5. Summary
      14. 7. Everything Is Connected – Memory Management
        1. Computer data storage
          1. File system
          2. Memory
        2. Value types versus reference types
          1. Determining value type or reference type
          2. Behavior on assignment
          3. Behavior on input
          4. Closure capture behavior
        3. Automatic reference counting
          1. Object relationships
            1. Strong
            2. Weak
            3. Unowned
        4. Strong reference cycles
          1. Between objects
            1. Spotting
            2. Fixing
          2. With closures
            1. Spotting
            2. Fixing
        5. Lost objects
          1. Between objects
          2. With closures
        6. Structures versus classes
        7. Summary
      15. 8. Paths Less Traveled – Error Handling
        1. Throwing errors
          1. Defining an error type
          2. Defining a function that throws an error
          3. Implementing a function that throws an error
        2. Handling errors
          1. Forceful try
          2. Optional try
          3. Catching an error
          4. Propagating errors
        3. Cleaning up in error situations
          1. Order of execution when errors occur
          2. Deferring execution
        4. Summary
      16. 9. Writing Code the Swift Way – Design Patterns and Techniques
        1. What is a design pattern?
        2. Behavioral patterns
          1. Iterator
          2. Observer
            1. Callback
            2. Notification center
        3. Structural patterns
          1. Composite
            1. Hierarchies
            2. Alternative to subclassing
          2. Delegate
          3. Model view controller
        4. Creational patterns
          1. Singleton/shared instance
          2. Abstract factory
        5. Using associated values effectively
          1. Replacing class hierarchies
          2. Concisely representing state
        6. Extending system types to reduce code
        7. Lazy properties
          1. Avoiding unnecessary memory usage
          2. Avoiding unnecessary processing
          3. Localizing logic to the concerned property
        8. Summary
      17. 10. Harnessing the Past – Understanding and Translating Objective-C
        1. Swift's relationship to Objective-C
        2. Background of Objective-C
        3. Constants and variables
          1. Value types
          2. Reference types
        4. Containers
          1. Arrays
          2. Dictionaries
        5. Control flow
          1. Conditionals
          2. Switches
          3. Loops
        6. Functions
        7. Types
          1. Structures
          2. Enumerations
          3. Classes
            1. Basic class
            2. Initializers
            3. Properties
            4. Methods
            5. Inheritance
          4. Categories
          5. Protocols
          6. Blocks
        8. Projects
          1. Header files
          2. Implementation files
          3. Organization
        9. Calling Objective-C code from Swift
          1. Bridging header
        10. Using functions
          1. Using types
          2. Containers
          3. Annotations
            1. Nullability
            2. Container element types
        11. Summary
      18. 11. A Whole New World – Developing an App
        1. Conceptualizing the app
          1. Features
          2. Interface
          3. Data
        2. Setting up the app project
        3. Configuring the user interface
        4. Running the app
        5. Allowing picture taking
        6. Temporarily saving a photo
        7. Populating our photo grid
        8. Refactoring to respect model-view-controller
        9. Permanently saving a photo
        10. Summary
      19. 12. What's Next? – Resources, Advice, and the Next Steps
        1. Apple's documentation
        2. Forums and blogs
          1. Blog posts
          2. Forums
        3. Prominent figures
        4. Podcasts
        5. Summary
      20. Index