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
Events
Videos
Audiobooks
Packt Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Object???Oriented Programming with Swift 2

You're reading from   Object???Oriented Programming with Swift 2 Get to grips with object-oriented programming in Swift to efficiently build powerful real-world applications

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher
ISBN-13 9781785885693
Length 332 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gastón C. Hillar Gastón C. Hillar
Author Profile Icon Gastón C. Hillar
Gastón C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Objects from the Real World to Playground 2. Structures, Classes, and Instances FREE CHAPTER 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 A. Exercise Answers Index

Using map to transform values


The map method takes a closure as an argument, calls it for each item in the array, and returns a mapped value for the item. The returned mapped value can be of a different type from the item's type.

The following lines declare a new getGamesNames method for our previously coded GameRepository class that performs the simplest map operation:

public func getGamesNames() -> [String] {
    return getAll().map({ game in game.name.uppercaseString })
}

The getGamesNames parameterless method returns Array<String>. The code calls the getAll method and calls the map method for the result with a closure that returns the name value for each game converted to uppercase. This way, the map method transforms each Game instance into a String with its name converted to uppercase. The result is an Array<String> generated by the call to the map method.

The following line uses the GameRepository instance called gameRepository to call the previously added getGamesNames...

lock icon The rest of the chapter is locked
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
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
Modal Close icon
Modal Close icon