DEV Community

loading...
Cover image for Day 8 out of 100 Days of SwiftUI

Day 8 out of 100 Days of SwiftUI

ramsescabello profile image Ramsés Cabello⚡️ ・1 min read

Structs look like a great way to organize data. Slowly getting to understand all the different concepts and how they're coming together.

Here are my notes from today:

Structs

Structs let you create our own data types our of several small types. Structs are common on SwiftUI because every piece of the UI is built around structs.

struct StructName {
    var variableName: DataType
    var variableName: DataType
    var variableName: DataType
}
Enter fullscreen mode Exit fullscreen mode

So, use tuples when you want to return two or more arbitrary pieces of values from a function, but prefer structs when you have some fixed data you want to send or receive multiple times.

Computed properties

This type of properties are properties that can run code to figure out the value.

struct StructName {
    var variableName: DataType {
        code
    }
}
Enter fullscreen mode Exit fullscreen mode

Property observers

This allows you to run code before or after a value changes.

Use didSet to run after a property has changed. The other command willSet will make the code run before a property changes.

Functions inside structs

Structs can also run functions inside:

struct StructName {
    var variableName: DataType

    func functionName() {
        return code
    }
}
Enter fullscreen mode Exit fullscreen mode

Use the word mutating before the function if the value inside the function can be changed.

Discussion (0)

pic
Editor guide