Swift Properties

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 String type with default value ""
  • age - of Int type 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

class Person {

 // define two properties
 var name: String = ""
 var age: Int = 0
}

var person1 = Person()

// assign values to properties
person1.name = "Kevin"
person1.age = 42

// access properties
print("Name:", person1.name)
print("Age:", person1.age)

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 property
  • person1.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

class Calculator {

  // define two stored properties
  var num1: Int = 0
  var num2: Int = 0

  // define one computed property
  var sum: Int {

    // calculate value
    num1 + num2
  }
}

var obj = Calculator()
obj.num1 = 11
obj.num2 = 12

// read value of computed property 
print("Sum:", obj.sum)

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,

class Calculator {
  var num1: Int = 0
  var num2: Int = 0

  // create computed property
  var sum: Int {

    // retrieve value
    get {
      num1 + num2
    }
  
    // set new value to num1 and num2
    set(modify) {
      num1 = (modify + 10)
      num2 = (modify + 20)
    }
  }
}

var obj = Calculator()
obj.num1 = 20
obj.num2 = 50

// get value
print("Get value:", obj.sum)

// provide value to modify
obj.sum = 5

print("New num1 value:", obj.num1)
print("New num2 value:", obj.num2)

 

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 getter
  • obj.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

class University {

 // static property
 static var name: String = ""

 // non-static property
 var founded: Int = 0
}

// create an object of University class
var obj = University()

// assign value to static property
University.name = "Kathmandu University"
print(University.name)

// assign value to non-static property
obj.founded = 1991
print(obj.founded)

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 name
  • obj.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.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges