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
}
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
}
}
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
}
}
Use the word mutating
before the function if the value inside the function can be changed.
Top comments (0)