Day 9 of 100 days of SwiftUI

sanmiade

Oluwasanmi Aderibigbe

Posted on March 23, 2021

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 ;)

💖 💪 🙅 🚩
sanmiade
Oluwasanmi Aderibigbe

Posted on March 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 10 of 100 days of SwiftUI
swift Day 10 of 100 days of SwiftUI

March 24, 2021

Day 9 of 100 days of SwiftUI
swift Day 9 of 100 days of SwiftUI

March 23, 2021

Day 6 of 100 days of SwiftUI
swift Day 6 of 100 days of SwiftUI

March 20, 2021

Day 5 of 100 days of SwiftUI
100daysofcode Day 5 of 100 days of SwiftUI

March 19, 2021

Day 4 of 100 days of SwiftUI
swift Day 4 of 100 days of SwiftUI

March 18, 2021