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

Swift 3 ObjectOriented Programming - Second Edition

Book Description

Implement object-oriented programming paradigms with Swift 3.0 and mix them with modern functional programming techniques to build powerful real-world applications

About This Book

  • Leverage the most efficient object-oriented design patterns in your Swift applications
  • Write robust, safer, and better code using the blueprints that generate objects
  • Build a platform with object-oriented code using real-world elements and represent them in your apps

Who This Book Is For

This book is for iOS and macOS developers who want to get a detailed practical understanding of object-oriented programming with the latest version of Swift: 3.0.

What You Will Learn

  • Write high-quality and easy-to-maintain reusable object-oriented code to build applications for iOS, macOS, and Linux
  • Work with encapsulation, abstraction, and polymorphism using Swift 3.0
  • Work with classes, instances, properties, and methods in Swift 3.0
  • Take advantage of inheritance, specialization, and the possibility to overload or override members
  • Implement encapsulation, abstraction, and polymorphism
  • Explore functional programming techniques mixed with object-oriented code in Swift 3.0
  • Understand the differences between Swift 3.0, previous Swift versions, and Objective-C code

In Detail

Swift has quickly become one of the most-liked languages and developers' de-facto choice when building applications that target iOS and macOS. In the new version, the Swift team wants to take its adoption to the next level by making it available for new platforms and audiences.

This book introduces the object-oriented paradigm and its implementation in the Swift 3 programming language to help you understand how real-world objects can become part of fundamental reusable elements in the code. This book is developed with XCode 8.x and covers all the enhancements included in Swift 3.0. In addition, we teach you to run most of the examples with the Swift REPL available on macOS and Linux, and with a Web-based Swift sandbox developed by IBM capable of running on any web browser, including Windows and mobile devices.

You will organize data in blueprints that generate instances. You'll work with examples so you understand how to encapsulate and hide data by working with properties and access control. Then, you'll get to grips with complex scenarios where you use instances that belong to more than one blueprint. You'll discover the power of contract programming and parametric polymorphism. You'll combine generic code with inheritance and multiple inheritance. Later, you'll see how to combine functional programming with object-oriented programming and find out how to refactor your existing code for easy maintenance.

Style and approach

This simple guide is packed with practical examples of solutions to common problems. Each chapter includes exercises and the possibility for you to test your progress by answering a quiz

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. Swift 3 ObjectOriented Programming - Second Edition
    1. Swift 3 ObjectOriented Programming - Second Edition
    2. Credits
    3. About the Author
    4. Acknowledgement
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Customer Feedback
    8. 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
    9. 1. Objects from the Real World to the Playground
      1. Installing the required software on Mac OS
      2. Installing the required software on Ubuntu Linux
      3. Working with Swift 3 on the web
      4. Capturing objects from the real world
      5. Generating classes to create objects
      6. Recognizing variables and constants to create properties
      7. Recognizing actions to create methods
      8. Organizing classes with UML diagrams
      9. Working with API objects in the Xcode Playground
      10. Exercises
      11. Test your knowledge
      12. Summary
    10. 2. Structures, Classes, and Instances
      1. Understanding structures, classes, and instances
      2. Understanding initialization and its customization
      3. Understanding deinitialization and its customization
      4. Understanding automatic reference counting
      5. Declaring classes
      6. Customizing initialization
      7. Customizing deinitialization
      8. Creating the instances of classes
      9. Exercises
      10. Test your knowledge
      11. Summary
    11. 3. Encapsulation of Data with Properties
      1. Understanding elements that compose a class
      2. Declaring stored properties
      3. Generating computed properties with setters and getters
      4. Combining setters, getters, and a related property
      5. Understanding property observers
      6. Transforming values with setters and getters
      7. Creating values shared by all the instances of a class with type properties
      8. Creating mutable classes
      9. Building immutable classes
      10. Exercises
      11. Test your knowledge
      12. Summary
    12. 4. Inheritance, Abstraction, and Specialization
      1. Creating class hierarchies to abstract and specialize behavior
      2. Understanding inheritance
      3. Declaring classes that inherit from another class
      4. Overriding and overloading methods
      5. Overriding properties
      6. Controlling whether subclasses can or cannot override members
      7. Working with typecasting and polymorphism
      8. Taking advantage of operator overloading
      9. Declaring compound assignment operator functions
      10. Declaring unary operator functions
      11. Declaring operator functions for specific subclasses
      12. Exercises
      13. Test your knowledge
      14. Summary
    13. 5. Contract Programming with Protocols
      1. Understanding how protocols work in combination with classes
      2. Declaring protocols
      3. Declaring classes that adopt protocols
      4. Taking advantage of the multiple inheritance of protocols
      5. Combining inheritance and protocols
      6. Working with methods that receive protocols as arguments
      7. Downcasting with protocols and classes
      8. Treating instances of a protocol type as a different subclass
      9. Specifying requirements for properties
      10. Specifying requirements for methods
      11. Combining class inheritance with protocol inheritance
      12. Exercises
      13. Test your knowledge
      14. Summary
    14. 6. Maximization of Code Reuse with Generic Code
      1. Understanding parametric polymorphism and generic code
      2. Declaring a protocol to be used as a constraint
      3. Declaring a class that conforms to multiple protocols
      4. Declaring subclasses that inherit the conformance to protocols
      5. Declaring a class that works with a constrained generic type
      6. Using a generic class for multiple types
      7. Combining initializer requirements in protocols with generic types
      8. Declaring associated types in protocols
      9. Creating shortcuts with subscripts
      10. Declaring a class that works with two constrained generic types
      11. Using a generic class with two generic type parameters
      12. Inheriting and adding associated types in protocols
      13. Generalizing existing classes with generics
      14. Extending base types to conform to custom protocols
      15. Test your knowledge
      16. Exercises
      17. Summary
    15. 7. Object-Oriented and Functional Programming
      1. Refactoring code to take advantage of object-oriented programming
      2. Understanding functions as first-class citizens
      3. Working with function types within classes
      4. Creating a functional version of array filtering
      5. Writing equivalent closures with simplified code
      6. Creating a data repository with generics and protocols
      7. Filtering arrays with complex conditions
      8. Using map to transform values
      9. Combining map with reduce
      10. Chaining filter, map, and reduce
      11. Solving algorithms with reduce
      12. Exercises
      13. Test your knowledge
      14. Summary
    16. 8. Extending and Building Object-Oriented Code
      1. Putting together all the pieces of the object-oriented puzzle
      2. Adding methods with extensions
      3. Adding computed properties to a base type with extensions
      4. Declaring new convenience initializers with extensions
      5. Defining subscripts with extensions
      6. Working with object-oriented code in iOS apps
      7. Adding an object-oriented data repository to a project
      8. Interacting with an object-oriented data repository through Picker View
      9. Exercises
      10. Test your knowledge
      11. Summary
    17. 9. Exercise Answers
      1. Chapter 1, Objects from the Real World to Playground
      2. Chapter 2, Structures, Classes, and Instances
      3. Chapter 3, Encapsulation of Data with Properties
      4. Chapter 4, Inheritance, Abstraction, and Specialization
      5. Chapter 5, Contract Programming with Protocols
      6. Chapter 6, Maximization of Code Reuse with Generic Code
      7. Chapter 7, Object-Oriented Programming and Functional Programming
      8. Chapter 8, Protection and Organization of Code