Object Oriented Programming I
What is an object? Declaration and definition.
Object Oriented Programming I
What is an object? Declaration and definition.
0
0
Checkbox to mark video as read
Mark as read

What is an object?

Understanding what an object is and using it properly is possibly the most important thing to becoming a professional developer in any language.

An object is a type of variable defined by us. Something similar to an enum, but with many more possibilities.

What is an object used for?

Let's look at an example of how using objects can help us. Imagine we want to display a list of people, and each person has a name and an age. With what we've seen so far, we could use two Arrays, one for the names and another for the ages:

var names: [String] = ["Pablo", "Andrea"]
var ages: [Int] = [36, 34]

The Arrays are ordered, and each name would correspond to the age in the same position. The problem is that both Arrays would have to be updated simultaneously because, for example, if we remove an item from names and not from ages, the data would no longer match.

To solve this, we will create a custom object. We can define as many objects as we need.

Declaring an object

Since we will use our object to store people's information, we will call it Person, meaning this object represents a person for whom we have their name and age.

To declare our Person object, we can use the keyword struct followed by the name we've chosen:

struct Person {
    var name: String
    var age: Int
}

Inside this object, between curly braces { }, we have added the variables name and age, which we will now call "properties." We should not assign values to these properties since this is just a kind of template that we will use to create each person info.

Creating our array of people

var pablo = Person(name: "Pablo", age: 36)
var andrea = Person(name: "Andrea", age: 34)

As you can see, we can now create a variable (or instance) for each person by writing the object name (Person) and its properties. This is known as a constructor, and each property included in our struct will also be included in the constructor.

Finally, if we wanted to have an array of people, we would simply use these variables as if they were a String, Int, or Bool.

var people: [Person] = [pablo, andrea]

Accessing each person's properties

To access the name or age of each person and display them on the screen, we use the dot symbol (.). This is used both to get the value and to assign a new value.

Text(pablo.name)
Text(andrea.name)

pablo.age = 37
andrea.age = 35

Practical example

To fully understand this concept, let's see how it would be applied in a view to display a list of people to our users.

@State var listPeople: [Person] = [
    Person(name: "Pablo", age: 36),
    Person(name: "Andrea", age: 34)
]

struct Person: Hashable {
    var name: String
    var age: Int
}

var body: some View {
    List {
        ForEach(listPeople, id: \.self) { person in
            HStack {
                Text(person.name)
                Spacer()
                Text("\(person.age)")
            }
        }
    }
}


course

Quiz Time!

0 Comments

Join the community to comment
Sign Up
I have an account
Be the first to comment

Accept Cookies

We use cookies to collect and analyze information on site performance and usage, in order to provide you with better service.

Check our Privacy Policy