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
Explore 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
Swift 3 Object-Oriented Programming
Swift 3 Object-Oriented Programming

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 , Second Edition

Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (1 Ratings)
Paperback Feb 2017 370 pages 2nd Edition
eBook
€23.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (1 Ratings)
Paperback Feb 2017 370 pages 2nd Edition
eBook
€23.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€23.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Swift 3 Object-Oriented Programming

Chapter 1. Objects from the Real World to the Playground

Whenever you have to solve a problem in the real world, you use elements and interact with them. For example, when you are thirsty, you take a glass, you fill it up with water, soda, or your favorite juice, and then you drink. Similarly, you can easily recognize elements, known as objects, from real-world actions and then translate them into object-oriented code. In this chapter, we will start learning the principles of object-oriented programming to use them in Swift 3 to develop apps and applications.

Installing the required software on Mac OS

In this book, you will learn to take advantage of all the object-oriented features included in Swift programming language version 3. Some of the examples might be compatible with previous Swift versions, such as 2.3, 2.2, 2.1, and 2.0, but it is essential to use Swift 3.0 or later because this version is not backward compatible. We won't write code that is backwards compatible with previous Swift versions because our main goal is to work with Swift 3.0 or later and to use its syntax.

We will use Xcode as our Integrated Development Environment (IDE). All the examples work with Xcode version 8 or higher. The latest versions of the IDE include Swift 3 as one of the supported programming languages to build iOS apps, watchOS apps, tvOS apps, and Mac OS applications. It is important to note that Xcode only runs on Mac OS, and all the instructions provided in this chapter consider that we are running this operating system on a Mac computer. However, after Apple launched Swift 2.2, it made the language open source and added a port to Linux, specifically to Ubuntu. Swift 3 is also available on Ubuntu. Thus, we can apply everything we learn about object-oriented programming with Swift when targeting other platforms to which the language is ported.

Tip

In case you want to work with the Swift open source release on Mac OS, you can download the latest release in the Downloads section at http://swift.org. You can run all the code examples included in this book in the Swift Read Evaluate Print Loop command-line environment instead of working with Xcode Playground. The Swift Read Evaluate Print Loop command-line environment is also known as Swift REPL.

It is also possible to use the Swift Playgrounds app on iOS 10.0 or later in the iPad models that are compatible with this app. You can work with this app to run the examples. However, our main IDE will be Xcode.

The following is the URL for the Swift Playgrounds app: https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=908519492

In order to install Xcode, you just need to launch the Mac App Store, enter Xcode in the search box, click on the Xcode application icon shown in the results, and make sure that it is the application developed by Apple and not an Xcode helper application. The following screenshot shows the details of the Xcode application in the Mac App Store:

Installing the required software on Mac OS

Then, click on Get and wait until the Mac App Store downloads Xcode. Note that it is necessary to download a few GBs and therefore it may take some time to finish the download process. Once the download is finished, click on Install and follow the necessary steps to complete the application's installation process. Finally, you will be able to launch the Xcode application as you would execute any other application in your Mac OS operating system. It is also possible to download and install Xcode from http://developer.apple.com/xcode/.

Apple usually launches Xcode beta versions before releasing the final stable versions. It is highly recommended to avoid working with beta versions to test the examples included in this book because beta versions are unstable and some examples might crash or generate unexpected outputs. The Mac App Store only offers the latest stable version of Xcode, and therefore, there is no risk of installing a beta version by mistake when following the previously explained steps.

In case we have any Xcode beta version installed on the same computer in which we will run the book samples, we have to make sure that the configuration for the stable Xcode version uses the appropriate command-line tools. We won't work with the command-line tools, but we will take advantage of Playground, and this feature uses the command-line tools under the hood.

Launch Xcode, navigate to Xcode | Preferences..., and click on Locations. Make sure that the Command Line Tools drop-down menu displays the stable Xcode version that you installed as the selected option. The following screenshot shows Xcode 8.0 (8A218a) as the selected version for Command Line Tools. However, you will definitely see a higher version number because Xcode is updated frequently:

Installing the required software on Mac OS

Tip

We don't need an iOS Developer Program membership to run the examples included in this book. However, in case we want to distribute the apps or applications coded in Swift to any App Store or activate certain capabilities in Xcode, we will require an active membership.

You don't need any previous experience with the Swift programming language to work with the examples in this book and learn how to model and create object-oriented code with Swift 3. If you have some experience with Objective-C, Java, C#, Python, Ruby, or JavaScript, you will be able to easily learn Swift's syntax and understand the examples. Swift borrows many features from these and other modern programming languages, and therefore, any knowledge of these languages will be extremely useful.

Installing the required software on Ubuntu Linux

In case we want to work with Swift 3 in Ubuntu Linux, we won't be able to run the examples that interact with any iOS API. However, we will be able to run a big percentage of the sample code included in this book, and we will be able to learn the most important object-oriented principles.

We can download the latest release for our Ubuntu version in the DOWNLOAD section at http://swift.org. This page includes all the instructions to install the required dependencies (clang and libicu-dev) and to execute the Swift REPL command-line environment.

Once we have completed the installation, we can execute the swift command to run the REPL in a Terminal. After we see a welcome message, we can enter Swift code and the REPL will display the results of executing each code block. We can also take advantage of a set of LLDB debugging commands. We just need to enter :help to list all the available debugger commands.

The following screenshot shows the Terminal application in Ubuntu running the swift command and displaying the results after entering two lines of Swift code:

Installing the required software on Ubuntu Linux

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 27, 2017
Length: 370 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787120396
Vendor :
Apple
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 27, 2017
Length: 370 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787120396
Vendor :
Apple
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 106.97
Swift 3 Object-Oriented Programming
€32.99
Mastering Swift 3
€36.99
Swift 4 Programming Cookbook
€36.99
Total 106.97 Stars icon

Table of Contents

9 Chapters
1. Objects from the Real World to the Playground Chevron down icon Chevron up icon
2. Structures, Classes, and Instances Chevron down icon Chevron up icon
3. Encapsulation of Data with Properties Chevron down icon Chevron up icon
4. Inheritance, Abstraction, and Specialization Chevron down icon Chevron up icon
5. Contract Programming with Protocols Chevron down icon Chevron up icon
6. Maximization of Code Reuse with Generic Code Chevron down icon Chevron up icon
7. Object-Oriented and Functional Programming Chevron down icon Chevron up icon
8. Extending and Building Object-Oriented Code Chevron down icon Chevron up icon
9. Exercise Answers Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
(1 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 100%
frankieho120 Aug 02, 2018
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I think it is a very poor book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.