Introduction

Welcome to the Swift Objects tutorial, which is part of our Swift series. In Swift, everything is treated as an object, even basic values like numbers. This means that every value can receive messages and have associated functions called methods.

Messages and Methods

In Swift, each type can have multiple methods associated with it. For example, the number value 8 has a isMultiple method that can be called to check if it is a multiple of another number.

let number = 8
let isMultiple = number.isMultiple(of: 2)
// Output: true

Similarly, a String value has its own set of methods. For instance, the String type has an instance variable called count which returns the number of characters in a string.

let message = "Hello, World!"
let characterCount = message.count
// Output: 13

Object Types in Swift

Swift provides three different object types: classes, structs, and enums. These types have their distinct characteristics and purposes. We’ll delve deeper into each of them in later tutorials. However, they all share a common feature: the ability to add methods. Additionally, any value of any object type can receive messages.

Conclusion

Understanding objects in Swift is fundamental to grasp the concept of object-oriented programming. In Swift, everything is treated as an object, allowing values to receive messages and have associated methods. Stay tuned for our upcoming tutorials, where we’ll explore classes, structs, and enums in more detail.