Object–Oriented Programming with Swift 2
eBook - ePub

Object–Oriented Programming with Swift 2

  1. 332 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Object–Oriented Programming with Swift 2

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Object–Oriented Programming with Swift 2 by Gaston C. Hillar in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2016
ISBN
9781785885693
Edition
1

Object–Oriented Programming with Swift 2


Table of Contents

Object–Oriented Programming with Swift 2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
eBooks, discount offers, and more
Questions
1. Objects from the Real World to Playground
Installing the required software
Capturing objects from the real world
Generating classes to create objects
Recognizing variables and constants to create properties
Recognizing actions to create methods
Organizing classes with UML diagrams
Working with API objects in the Xcode Playground
Exercises
Test your knowledge
Summary
2. Structures, Classes, and Instances
Understanding structures, classes, and instances
Understanding initialization and its customization
Understanding deinitialization and its customization
Understanding automatic reference counting
Declaring classes
Customizing initialization
Customizing deinitialization
Creating the instances of classes
Exercises
Test your knowledge
Summary
3. Encapsulation of Data with Properties
Understanding the elements that compose a class
Declaring stored properties
Generating computed properties with setters and getters
Combining setters, getters, and a related property
Understanding property observers
Transforming values with setters and getters
Using type properties to create values shared by all the instances of a class
Creating mutable classes
Building immutable classes
Exercises
Test your knowledge
Summary
4. Inheritance, Abstraction, and Specialization
Creating class hierarchies to abstract and specialize behavior
Understanding inheritance
Declaring classes that inherit from another class
Overriding and overloading methods
Overriding properties
Controlling whether subclasses can or cannot override members
Working with typecasting and polymorphism
Taking advantage of operator overloading
Declaring operator functions for specific subclasses
Exercises
Test your knowledge
Summary
5. Contract Programming with Protocols
Understanding how protocols work in combination with classes
Declaring protocols
Declaring classes that adopt protocols
Taking advantage of the multiple inheritance of protocols
Combining inheritance and protocols
Working with methods that receive protocols as arguments
Downcasting with protocols and classes
Treating instances of a protocol type as a different subclass
Specifying requirements for properties
Specifying requirements for methods
Combining class inheritance with protocol inheritance
Exercises
Test your knowledge
Summary
6. Maximization of Code Reuse with Generic Code
Understanding parametric polymorphism and generic code
Declaring a protocol to be used as a constraint
Declaring a class that conforms to multiple protocols
Declaring subclasses that inherit the conformance to protocols
Declaring a class that works with a constrained generic type
Using a generic class for multiple types
Combining initializer requirements in protocols with generic types
Declaring associated types in protocols
Creating shortcuts with subscripts
Declaring a class that works with two constrained generic types
Using a generic class with two generic type parameters
Inheriting and adding associated types in protocols
Generalizing existing classes with generics
Extending base types to conform to custom protocols
Test your knowledge
Exercises
Summary
7. Object-Oriented Programming and Functional Programming
Refactoring code to take advantage of object-oriented programming
Understanding functions as first-class citizens
Working with function types within classes
Creating a functional version of array filtering
Writing equivalent closures with simplified code
Creating a data repository with generics and protocols
Filtering arrays with complex conditions
Using map to transform values
Combining map with reduce
Chaining filter, map, and reduce
Solving algorithms with reduce
Exercises
Test your knowledge
Summary
8. Extending and Building Object-Oriented Code
Putting together all the pieces of the object-oriented puzzle
Adding methods with extensions
Adding computed properties to a base type with extensions
Declaring new convenience initializers with extensions
Defining subscripts with extensions
Working with object-oriented code in apps
Adding an object-oriented data repository to a project
Interacting with an object-oriented data repository through Picker View
Exercises
Test your knowledge
Summary
A. Exercise Answers
Chapter 1, Objects from the Real World to Playground
Chapter 2, Structures, Classes, and Instances
Chapter 3, Encapsulation of Data with Properties
Chapter 4, Inheritance, Abstraction, and Specialization
Chapter 5, Contract Programming with Protocols
Chapter 6, Maximization of Code Reuse with Generic Code
Chapter 7, Object-Oriented Programming and Functional Programming
Chapter 8, Extending and Building Object-Oriented Code
Index

Object–Oriented Programming with Swift 2

Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2016
Production reference: 1220116
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978...

Table of contents

  1. Object–Oriented Programming with Swift 2