A Swift variable or constant defined inside a class or struct are called properties. For example,
class Person {
// define properties
var name: String = ""
var age: Int = 0
...
}
Here, inside the Person class we have defined two properties:
- name - of
Stringtype with default value"" - age - of
Inttype with default value 0
Before you learn about properties, make sure you know the working of class and struct in Swift.
Example 1: Swift Properties
Output
Name: Kevin Age: 42
In the above example, we have created a class named Person with two properties: name and age.
Here, we have used the object person1 to access the properties:
person1.name- access the name propertyperson1.age- access the age property
Note: These properties we defined above are also called stored properties as they store actual values for each instance of the Person class.
Swift Computed Property
Earlier, we discussed about stored properties that store value for each instance. For example,
class Calculator {
// define stored property
var num1: Int = 0
...
}
Here, num1 is a stored property, which stores some value for an instance of Calculator.
However, there is another type of property called computed properties. Unlike stored property, a computed property calculates the value. For example,
class Calculator {
...
// define computed property
var sum: Int {
// calculate value
num1 + num2
}
}
Here, sum is a computed property that doesn't store a value, rather it computes the addition of two values.
Note: The curly braces {..} indicates the body of the computed property.
Example: Swift Computed Property
Output
Sum: 23
In the above example, we have
- Stored properties: num1 and num2
- Computed Property: sum.
Here, the computed property sum computes the addition of num1 and num2
var sum: Int {
// calculate value
num1 + num2
}
Now, we can access the computed value by accessing the computed property
print("Sum:", obj.sum)
Getters And Setters for Computed Properties
In Swift, we can also include a getter and setter inside a computed property.
- getter - returns the computed value
- setter - changes the value
Let's see an example,
Output
Get value: 70 New num1 value: 15 New num2 value: 25
In the above example, we have created a computed property named sum. Inside the sum, we have
1. Getter - to get the sum of num1 and num2
get {
num1 + num2
}
2. Setter - to change value of num1 and num2
set(modify) {
num1 = (modify + 10)
num2 = (modify + 20)
}
Here, the setter has a new value named modify that can be used to set new values to num1 and num2.
Finally, we have used,
obj.sum- to access getterobj.sum = 5- to access setter
Swift Static Properties
In the previous example, we have used objects of the class to access its properties. However, we can also create properties that can be accessed and modified without creating objects.
These types of properties are called static properties. In Swift, we use the static keyword to create a static property. For example,
class University {
// static property
static var name: String = ""
...
}
Here, name is the static property. Now to access and modify the value of a static property, we use the class name.
// modify value of the static property
University.name = "Kathmandu University"
Swift static Property
Output
Kathmandu University 1991
In the above example, we have a static property: name and a non-static property - founded Here,
University.name- access the static property using class nameobj.founded- access the non-static property using the object of the class
A static property is of the class type (associated with class rather than object), so we are able to access them using class names.
Note: Similarly, we can also create static properties inside a struct. static properties inside a struct are of a struct type, so we use the struct name to access them.