Classes can define properties that instances of the class can use. In this example, Dog
has two properties: name
and dogYearAge
:
class Dog {
var name = ""
var dogYearAge = 0
}
You can access the properties with dot syntax:
let dog = Dog()
print(dog.name)
print(dog.dogYearAge)
Classes can also define methods that can be called on the instances, they are declared similar to normal functions, just inside the class:
class Dog {
func bark() {
print("Ruff!")
}
}
Calling methods also uses dot syntax:
dog.bark()