Object-oriented programming is a common and powerful programming paradigm. At its core is the object class. Objects allow us to encapsulate data and functionality, which can then be stored and passed around.
In this recipe, we will build some class objects, break down their components, and understand how they are defined and used.
How to do it...
Let's write some code to create and use class objects, and then we will walk through what the code is doing:
- First, create a Person class object:
class Person {
}
- Within the curly brackets, { and }, add three constants representing the person's name, and one variable representing their country of residence:
let givenName: String
let middleName: String
let familyName: String
var countryOfResidence: String = "UK"
- Below the properties, but still within the curly brackets, add an initialization method for our Person object:
init(givenName: String, middleName: String, familyName...