define a struct depicting a car, with a brand, model and year properties, as well as a beep function that prints “beep” into the console in swift

main.swift
struct Car {
    let brand: String
    let model: String
    let year: Int
    
    func beep() {
        print("beep")
    }
}

let myCar = Car(brand: "Toyota", model: "Corolla", year: 2020)
myCar.beep()  // prints "beep" to the console
238 chars
13 lines

In the example above, we define a struct Car, which has three properties: brand and model are of type String, and year is of type Int. We also define a beep function that just prints "beep" to the console.

We create an instance of the Car struct with the let myCar = Car(brand: "Toyota", model: "Corolla", year: 2020) statement, passing in the brand, model, and year as arguments to the initializer. Finally, we call the beep function on the myCar instance with myCar.beep(), which prints "beep" to the console.

gistlibby LogSnag