Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Object???Oriented Programming with Swift 2
  • Table Of Contents Toc
Object???Oriented Programming with Swift 2

Object???Oriented Programming with Swift 2

By : Gastón C. Hillar
5 (6)
close
close
Object???Oriented Programming with Swift 2

Object???Oriented Programming with Swift 2

5 (6)
By: Gastón C. Hillar

Overview of this book

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.
Table of Contents (11 chapters)
close
close
10
Index

Working with function types within classes


The following lines declare a myFunction variable with a function type—specifically, a function that receives an Int argument and returns a Bool value. The variable works in the same way as an argument that specifies a function type for a function:

var myFunction: (Int -> Bool)
myFunction = divisibleBy5
let myNumber = 20
print("Is \(myNumber) divisible by 5: \(myFunction(myNumber))")

Then, the code assigns the divisibleBy5 function to myFunction. It is very important to understand that the line doesn't call the divisibleBy5 function and save the result of this call in the myFunction variable. Instead, it just assigns the function to the variable that has a function type. The lack of a parenthesis after the function name makes the difference.

Then, the code prints whether the Int value specified in the myNumber constant is divisible by 5 or not using the myFunction variable to call the referenced function with myNumber as an argument.

The following...

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object???Oriented Programming with Swift 2
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon