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

Object–Oriented Programming with Swift 2

Book Description

Get to grips with object-oriented programming in Swift to efficiently 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 by using real-world elements and represent them in your app

Who This Book Is For

If you are an iOS developer who has a basic idea of object-oriented programming and want to incorporate its concepts with Swift to optimize your application's code and create reusable and easily to understand building blocks, then this book is for you. This is a very useful resource for developers who want to shift from Objective C, C#, Java, Python, JavaScript, or other object-oriented languages to Swift

What You Will Learn

  • Build solid, stable, and reliable applications using Swift
  • Work with encapsulation, abstraction, and polymorphism using Swift 2.0
  • Customize constructors and destructors based on your needs
  • Develop Swift 2.0 with classes, instances, properties, and methods
  • Take advantage of generic code to maximize code reuse and generalize behaviors
  • Use state of inheritance, specialization, and the possibility to overload members
  • Write high quality object-oriented code to build apps for iOS or Mac OS X

In Detail

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects; these are data structures that contain data in the form of fields, often known as attributes and code. Objects are everywhere, and so it is very important to recognize elements, known as objects, from real-world situations and know how they can easily be translated into object-oriented code.

Object-Oriented Programming with Swift is an easy-to-follow guide packed full of hands-on examples of solutions to common problems encountered with object-oriented code in Swift. It starts by helping you to recognize objects using real-life scenarios and demonstrates how working with them makes it simpler to write code that is easy to understand and reuse.

You will learn to protect and hide data with the data encapsulation features of Swift. Then, you will explore how to maximize code reuse by writing code capable of working with objects of different types. After that, you'll discover the power of parametric polymorphism and will combine generic code with inheritance and multiple inheritance. Later, you move on to refactoring your existing code and organizing your source for easy maintenance and extensions.

By the end of the book, you will be able to create better, stronger, and more reusable code, which will help you build better applications.

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 questions.

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. Object–Oriented Programming with Swift 2
    1. Table of Contents
    2. Object–Oriented Programming with Swift 2
    3. Credits
    4. About the Author
    5. About the Reviewers
    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. eBooks, discount offers, and more
        5. Questions
    8. 1. Objects from the Real World to Playground
      1. Installing the required software
      2. Capturing objects from the real world
      3. Generating classes to create objects
      4. Recognizing variables and constants to create properties
      5. Recognizing actions to create methods
      6. Organizing classes with UML diagrams
      7. Working with API objects in the Xcode Playground
      8. Exercises
      9. Test your knowledge
      10. Summary
    9. 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
    10. 3. Encapsulation of Data with Properties
      1. Understanding the 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. Using type properties to create values shared by all the instances of a class
      8. Creating mutable classes
      9. Building immutable classes
      10. Exercises
      11. Test your knowledge
      12. Summary
    11. 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 operator functions for specific subclasses
      10. Exercises
      11. Test your knowledge
      12. Summary
    12. 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
    13. 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
    14. 7. Object-Oriented Programming 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
    15. 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 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
    16. A. 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, Extending and Building Object-Oriented Code
    17. Index