You are previewing Learning Swift.
O'Reilly logo
Learning Swift

Book Description

Build a solid foundation in Swift to develop smart and robust iOS and OS X applications

In Detail

Swift is Apple's new programming language and the future of iOS and OS X app development. At its surface, Swift is easy to jump into, but it has complex elements that are critical to becoming proficient at turning an idea into reality.

Develop the skills and confidence to dive passionately into creating compelling, maintainable, and elegant apps in Swift. This book provides an approachable, step-by-step introduction into programming with Swift. Each topic is separated into comprehensive sections that are full of practical examples and easy-to-understand explanations. Each section builds on the previous topics to develop a comprehensive understanding of app development in Swift.

It begins by giving an overview of the key features with practical examples and progresses to more advanced topics that help differentiate the skillful developers from the mediocre ones. It covers topics, such as variables, optionals, closures, generics, and memory management. In addition, this book also covers the art of programming, such as maintainability, useful design patterns, and resources for furthering 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 a reality.

What You Will Learn

  • Form a solid understanding of the Swift language

  • Learn the practical aspects of how a computer program actually works

  • Understand the paradigms used by Apple's frameworks and not be intimidated by them

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

  • Develop a basic portfolio of Swift code, covering and practicing critical concepts

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

  • Write and understand concise but expressive functional style code

  • Implement various Swift design patterns to solve real-world problems

  • Create your own generics for existing applications

  • 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. Learning Swift
      1. Table of Contents
      2. Learning Swift
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      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. Errata
          3. Piracy
          4. 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 on 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
        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 initializers
            2. Required initializers
            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
            1. 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 is scope defined
          2. Nested types
        7. Access control
        8. Summary
      11. 4. To Be or Not to Be – Optionals
        1. Introducing optionals
        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. The filter function
          2. The reduce function
          3. The map function
          4. The sorted function
          5. How these affect the state and nature of code
        4. Lazy evaluation
        5. Curried functions
        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. Defining a generic
          1. The generic function
          2. The generic type
          3. Type constraints
            1. Protocol constraints
            2. Where clauses for protocols
            3. Where clauses for equality
        3. Extending the existing generics
          1. Enhancing arrays
          2. Enhancing dictionaries
        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. How data is stored in a computer
          1. Filesystem
          2. Memory
        2. Value types versus reference types
          1. Determining a value or reference type
          2. Behavior on assignment
          3. Behavior on input
          4. Capturing behavior of closure
        3. Automatic reference counting
          1. Object relationships
            1. Strong relationships
            2. Weak relationships
            3. Unowned relationships
        4. Strong reference cycles
          1. Strong reference cycles between objects
            1. Spotting
            2. Fixing the cycles
          2. With closures
            1. Spotting
            2. Fixing
        5. Lost objects
          1. Between objects
          2. With closures
        6. Summary
      15. 8. Writing Code the Swift Way – Design Patterns and Techniques
        1. What is a design pattern?
        2. Behavioral patterns
          1. The iterator pattern
          2. The observer pattern
            1. The callback property
            2. The notification center
        3. Structural patterns
          1. The composite pattern
            1. Hierarchies
            2. An alternative to subclassing
          2. The delegate pattern
          3. Model-View-Controller
        4. Creational patterns
          1. A singleton/shared instance
          2. The abstract factory pattern
        5. Using associated values effectively
          1. Replacing class hierarchies
          2. Concisely representing a 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
      16. 9. Harnessing the Past – Understanding and Translating Objective-C
        1. Swift's relationship with Objective-C
        2. The history 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. The basic class
            2. Initializers
            3. Properties
            4. Methods
            5. Inheritance
          4. Categories
          5. Protocols
          6. Blocks
        8. Projects
          1. Header files
          2. An implementation file
          3. Organization
        9. Calling Objective-C code from Swift
          1. A bridging header
        10. Using functions
          1. Using types
          2. Containers
          3. Reference type translation
        11. Summary
      17. 10. A Whole New World – Developing an App
        1. Conceptualizing the app
          1. Features
          2. The 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
      18. 11. What's Next? Resources, Advice, and Next Steps
        1. Apple's documentation
        2. Forums and blogs
          1. Blog posts
          2. Forums
        3. Prominent figures
        4. Podcasts
        5. Summary
      19. Index