Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Swift 3 Object-Oriented Programming

You're reading from   Swift 3 Object-Oriented Programming Implement object-oriented programming paradigms with Swift 3.0 and mix them with modern functional programming techniques to build powerful real-world applications

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781787120396
Length 370 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Swift 3 ObjectOriented Programming - Second Edition
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Objects from the Real World to the Playground FREE CHAPTER 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 and Functional Programming 8. Extending and Building Object-Oriented Code 9. Exercise Answers

Preface

Object-oriented programming, also known as OOP, is a required skill in absolutely 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 multiparadigm 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 (Mac OS, iOS, tvOS, and watchOS operating systems) you need to master OOP in Swift 3. However, Swift 3 is not limited to Apple platforms and you can take advantage of your Swift 3 knowledge to develop applications that target other platforms and use it for server-side code. In addition, as Swift also grabs 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 3. You will learn the OOP 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 the Playground , covers the principles of object-oriented paradigms. We will understand how real-world objects can become part of fundamental elements in the code. We will translate elements into the different components of the object-oriented paradigm supported in Swift 3--classes, protocols, properties, methods, and instances. We will run examples in Xcode 8, the Swift REPL and a web-based Swift 3 sandbox.

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

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

Chapter 4 , Inheritance, Abstraction, and Specialization , helps you in 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 , delves into 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 , covers 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 , covers how to refactor existing code to take full advantage of object-oriented code. We will prepare the code for future requirements, reduce maintenance cost, and maximize code reuse. We will also work with many functional programming features included in Swift 3, combined with object-oriented programming.

Chapter 8 , Protection and Organization of 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 to 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 we will learn how everything we learned about object-oriented programming is useful in any kind of apps we might create.

What you need for this book

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

In order to work with Swift 3.x open source version in 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 have been built and tested. It it also possible to run the Swift compiler and utilities on other Linux distributions. You must check the latest available documentation at the Swift open source website: https://swift.org.

In order to work with the web-based IBM Swift Sandbox, you will need any device capable of executing a modern web browser.

Who this book is for

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

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 just need to enter :help to list all the available debugger commands."

A block of code is set as follows:

    let degCUnit = HKUnit.degreeCelsius() 
    let degFUnit = HKUnit.degreeFahrenheit()

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

    open func filteredBy(condition: (Int) -> Bool) -> [Int] { 
      return numbersList.filter({ condition($0) }) 
    }

Any command-line input or output is written as follows:

id: 1, name: "Invaders 2017", highestScore: 1050, playedCount: 3050

id: 2, name: "Minecraft", highestScore: 3741050, playedCount: 780009992

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: "Clicking the Next button moves you to the next screen."

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 [email protected], 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 for this book 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Swift-3-Object-Oriented-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

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 [email protected] with a link to the suspected pirated material.

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

Questions

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

lock icon The rest of the chapter is locked
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 ₹800/month. Cancel anytime
Visually different images