Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Object???Oriented Programming with Swift 2

You're reading from  Object???Oriented Programming with Swift 2

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781785885693
Pages 332 pages
Edition 1st Edition
Languages
Author (1):
Gaston C. Hillar Gaston C. Hillar
Profile icon Gaston C. Hillar

Table of Contents (16) Chapters

Object–Oriented Programming with Swift 2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Objects from the Real World to Playground 2. Structures, Classes, and Instances 3. Encapsulation of Data with Properties 4. Inheritance, Abstraction, and Specialization 5. Contract Programming with Protocols 6. Maximization of Code Reuse with Generic Code 7. Object-Oriented Programming and Functional Programming 8. Extending and Building Object-Oriented Code Exercise Answers Index

Preface

Object-oriented programming, also known as OOP, is a required skill in any modern software developer job. It makes a lot of sense because object-oriented programming allows you to maximize code reuse and minimize maintenance costs. However, learning object-oriented programming is challenging because it includes too many abstract concepts that require real-life examples to be easy to understand. In addition, object-oriented code that doesn't follow best practices can easily become a maintenance nightmare.

Swift is a multi-paradigm programming language, and one of its most important paradigms is OOP. If you want to create great applications and apps for Mac, iPhone, iPad, Apple TV, and Apple Watch, you need to master OOP in Swift. In addition, as Swift also grabs the nice features found in functional programming languages, it is convenient to know how to mix OOP code with functional programming code.

This book will allow you to develop high-quality reusable object-oriented code in Swift 2.2. You will learn the object-oriented programming principles and how Swift implements them. You will learn how to capture objects from real-world elements and create object-oriented code that represents them. You will understand Swift's approach towards object-oriented code. You will maximize code reuse and reduce maintenance costs. Your code will be easy to understand, and it will work with representations of real-life elements.

What this book covers

Chapter 1, Objects from the Real World to Playground, teaches you the principles of object-oriented paradigms. We will discuss how real-world objects can become part of the fundamental elements of code. We will translate elements into the different components of the object-oriented paradigm supported in Swift: classes, protocols, properties, methods, and instances.

Chapter 2, Structures, Classes, and Instances, starts generating blueprints to create objects. You will learn about an object's life cycle, and we will work with many examples to understand how object initializers and deinitializers work.

Chapter 3, Encapsulation of Data with Properties, introduces you to organizing data in the blueprints that generate objects. We will understand the different members of a class and how they are reflected by members of the instances generated from a class. You will learn the difference between mutable and immutable classes.

Chapter 4, Inheritance, Abstraction, and Specialization, introduces you to creating a hierarchy of blueprints that generate objects. We will take advantage of inheritance and many related features to specialize behavior.

Chapter 5, Contract Programming with Protocols, discusses how Swift works with protocols in combination with classes. We will declare and combine multiple blueprints to generate a single instance. We will declare protocols with different types of requirements, and then we will create classes that conform to these protocols.

Chapter 6, Maximization of Code Reuse with Generic Code, teaches you how to maximize code reuse by writing code capable of working with objects of different types—that is, instances of classes that conform to specific protocols or whose class hierarchy includes specific superclasses. We will work with protocols and generics.

Chapter 7, Object-Oriented Programming and Functional Programming, teaches you how to refactor existing code to take full advantage of object-oriented code. We will prepare the code for future requirements, reduce maintenance costs, and maximize code reuse. We will also work with many functional programming features included in Swift combined with object-oriented programming.

Chapter 8, Extending and Building Object-Oriented Code, puts together all the pieces of the object-oriented puzzle. We will take advantage of extensions to add features to types, classes, and protocols in which we don't have access to the source code. We will make sure that the code exposes only the things that it has to expose, and you will learn how everything you learned about object-oriented programming is useful in any kind of app we might create.

What you need for this book

In order to work with Xcode and the Swift Playground, you will need a Mac OS computer capable of running OS X 10.10.5 or later with 8 GB of RAM.

In order to work with the Swift open source version on the Linux platform, you will need any computer capable of running Ubuntu 14.04 or later or Ubuntu 15.10 or later. These are the Linux distributions where the Swift open source binaries are built and tested. It is also possible to run the Swift compiler and utilities on other Linux distributions. You must take a look at the latest available documentation at the Swift open source website, https://swift.org.

Who this book is for

If you are an IOS developer who has a basic idea of OOP and want to incorporate OOP concepts with Swift to optimize your application's performance, 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.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can assign 20 to regularHexagon1.lengthOfSide and 50 to regularHexagon2.lengthOfSide."

A block of code is set as follows:

let degCUnitFromStr = HKUnit(fromString: "degC")
let degFUnitFromStr = HKUnit(fromString: "degF")

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

Animal created
Mammal created
DomesticMammal created
Dog created
TerrierDog created
SmoothFoxTerrier created
I am 7 years old.
I am 14 years old.
I am 21 years old.
I am 4 years old.
I am 5 years old.

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Start Xcode, navigate to File | New | Playground…."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Object???Oriented Programming with Swift 2
Published in: Jan 2016 Publisher: ISBN-13: 9781785885693
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}