DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

Day 9 of 100 days of SwiftUI

Day 9 of HackingWithSwift's 100 days of SwiftUI. Today I learnt advance features of Structs such as access control, static properties, and laziness.

Lazy properties are a special kind of properties in Swift that are created only when they are needed. They are created the first time they are needed and reused later on. Lazy properties in Swift are written like this lazy multiplayer = Multiplayer().
Lazy properties are mostly used for properties that are not needed immediately the struct is created and are also take a bit of time to create.

Static properties and methods are properties and methods of the Struct itself not properties and methods of instances of the Struct. Static properties are used to store functionality that you use across your app.

struct CalculationUtils {
 static func addNumbers(numbers...) -> Int {
     var sum = 0
     for number in numbers {
       sum += number
     }
     return sum
  }
}

let sum = CalculationUtils.addNumbers(1,2,3,4)
Enter fullscreen mode Exit fullscreen mode

The code above creates a Struct with a static function addNumbers. Since addNumbers is a static function, it can be called directly as a method of CalculationUtils instead of first creating an instance of CalculationUtils as you would a normal function.

Access control is a Swift feature that helps you limit which code can call properties and method of your struct. As far as I know, there are two types of access control in Swift: private and public access control. Using the public access control member means every code has access to method and properties of your struct while using the private access control member means only code in that struct have access to its method and properties.

If you are interested in taking the challenge, you can find it at https://www.hackingwithswift.com/100/swiftui
See you tomorrow ;)

Top comments (0)