100 Days of SwiftUI – Day 11

·

Structs, part 2

How to limit access to internal data using access control

In order to have a private variable/const inside of a struct, just use the private keyword.

The point of the private keyword is to limit access to the variable inside of struct such that it can not be written to or read from outside of the struct.

This also reduces the surface area of the struct. Some properties are meant to be used/known outside of the struct, and others are internal so we shouldn’t use them.

If a struct has a private variable, it can not use the member-wise initializer.

Scored 6/12 on Access control

Thoughts: I’m not sure if its me or not but these little tests are becoming quite annoying not testing the actual material I just went over vs testing types or some other nuance. And when I’m done with a test like the one I just took, it doesn’t feel like I nailed the subject matter. There were questions that I got wrong because I can’t use member-wise to intialize a instance of a struct but then in the very next question its ok to use member-wise to initialize a struct with a private var inside. So which is it? I guess I have to start running the code in playground before answering because this is getting quite ridiculous.

Static properties and methods

The static keyword means that the property or method belongs to the struct, and not the instance of the struct. This allows you to use the property or method directly.

struct School {
    static var studentCount = 0

    static func add(student: String) {
        print("\(student) joined the school.")
        studentCount += 1
    }
}

We can use it like this:

School.add(student: "Taylor Swift")
print(School.studentCount)

Rules

  • you can not access non static code from static code.
  • to access static code from non-static code, always use your type ie School.studentCount or self

There are two self keywords. One is self which refers to the current value of the struct, and the other is Self which refers to the type.

Why? Example:

// using static properties to organize common data in my apps
struct AppData {
    static let version = "1.3 beta 2"
    static let saveFilename = "settings.json"
    static let homeURL = "https://www.hackingwithswift.com"
}

or:

struct Employee {
    let username: String
    let password: String
    // show an example employee in the preview screen 
    // so you can check it all looks correct as you work
    static let example = Employee(username: "cfederighi", password: "hairforceone")
}

Now when we need the Employee instance, we can use Employee.example for the preview.

Note: If we don’t need to create an instance of the struct, use a diff type ie. enum

Note: Referencing a static property inside a regular method isn’t allowed.

Scored 11/12 on Static properties and methods

Summary

Structs are used almost everywhere in Swift: StringIntDoubleArray and even Bool are all implemented as structs, and now you can recognize that a function such as isMultiple(of:) is really a method belonging to Int.

Let’s recap what else we learned:

  • You can create your own structs by writing struct, giving it a name, then placing the struct’s code inside braces.
  • Structs can have variable and constants (known as properties) and functions (known as methods)
  • If a method tries to modify properties of its struct, you must mark it as mutating.
  • You can store properties in memory, or create computed properties that calculate a value every time they are accessed.
  • We can attach didSet and willSet property observers to properties inside a struct, which is helpful when we need to be sure that some code is always executed when the property changes.
  • Initializers are a bit like specialized functions, and Swift generates one for all structs using their property names.
  • You can create your own custom initializers if you want, but you must always make sure all properties in your struct have a value by the time the initializer finishes, and before you call any other methods.
  • We can use access to mark any properties and methods as being available or unavailable externally, as needed.
  • It’s possible to attach a property or methods directly to a struct, so you can use them without creating an instance of the struct.

Currently
Building

1 product