Sobes.tech
Junior — Middle

Can you explain what is meant by an object in the Swift programming language?

sobes.tech AI

Answer from AI

In Swift, an object is an instance of a class or structure that contains data (properties) and behavior (methods). Objects allow modeling real entities and interacting with them through methods. For example, if you have a class Car, then an object would be a specific car with certain characteristics:

class Car {
    var color: String
    var model: String

    init(color: String, model: String) {
        self.color = color
        self.model = model
    }

    func drive() {
        print("The car is driving")
    }
}

let myCar = Car(color: "Red", model: "Tesla")
myCar.drive() // Outputs: The car is driving

Thus, an object is a specific instance with state and behavior, created based on the description in a class or structure.