What a field is
A field is a variable that belongs to a class, and every object made from the class gets its own copy. Swift's official name for it is a stored property. Without fields, every Dog would be identical; with fields, each one can carry its own name and age.
Declaring fields
class Dog {
var name: String = ""
var age: Int = 0
}
vardeclares a property that can be read and also assigned a new value. A property declared withletis a constant after it is set.name: Stringgives the property its type, and= ""gives it a default value.
Swift requires every stored property to have a value by the time an object exists. A default value satisfies that and also lets you write Dog() with no custom initializer. Without defaults the class doesn't compile:
class Dog {
var name: String
var age: Int
}
The compiler reports class 'Dog' has no initializers and notes that the properties without an initial value prevent a synthesized initializer. The constructors lesson shows how to write that initializer yourself.
Defaults are whatever you choose: 0 and "" are common, but there is no automatic zero. For a property that may legitimately have no value, use an optional type: var owner: String? starts as nil with no explicit default.
Reading and writing fields
The dot operator reaches a field of one particular object:
let dog = Dog()
dog.name = "Rex"
dog.age = 4
print("\(dog.name) is \(dog.age) years old")
This prints Rex is 4 years old. Each object has its own slot for each field, so two dogs can hold different names without interfering with one another.
Notice that dog is declared with let, yet dog.name = "Rex" compiles. For a class, the constant fixes which object dog refers to; the object's var properties can still change. Reassigning the constant itself (dog = Dog()) is an error: cannot assign to value: 'dog' is a 'let' constant. And a property declared with let inside the class can't be assigned from outside either: cannot assign to property: 'species' is a 'let' constant.
The exercise
The task has two steps. First, give Dog two var properties with default values, a String called name and an Int called age. Second, set them on the existing dog object before the print line:
class Dog {
var name: String = ""
var age: Int = 0
}
class Solution {
func mainFunction() {
let dog = Dog()
dog.name = "Rex"
dog.age = 4
print("\(dog.name) is \(dog.age) years old")
}
}
The output is Rex is 4 years old. The \(...) inside the string literal inserts the value of the expression into the text.
Common mistakes
- Omitting default values while also not writing an initializer, which produces the "no initializers" error above.
- Declaring the fields with
let. The assignmentsdog.name = "Rex"would then be rejected. - Setting the fields on the class instead of the object.
Dog.nameis not valid; fields belong to objects, so usedog.name. - Forgetting that objects share through references. If
let other = dog, thenother.name = "Max"also changesdog.name, because both names point to one object.
Key takeaways
- A field, or stored property, is a variable inside a class; each object has its own.
- Give every stored property a default value, or set it in an initializer.
- Use the dot operator to read and write:
dog.name. - A
letconstant holding a class object still allows itsvarproperties to change.
