© Yanis Zafirópulos 2019
Yanis ZafirópulosSwift 4 Recipeshttps://doi.org/10.1007/978-1-4842-4182-0_6

6. Classes and Closures

Yanis Zafirópulos1 
(1)
Granada, Spain
 

In this chapter, we’re going to explore two of the most powerful elements of the Swift language: classes and closures. Classes are a neat way to group functions together, along with data, and be able to create integrated objects with “state.” While closures provide us with a flexible way to use functions, without the usual function overhead – that is, defining them thoroughly, naming them, declaring their arguments, etc.

6.1 Define a basic class

Problem

We want to define a barebones class .

Solution

So, let’s start, by defining a new Person class.
class Person {
      // When a new instance is ...

Get Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.