An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
100 Days of Swift - Day 8 (and 9)
Continuation of Hacking With Swift 100 Days of Swift
Day 8 (and 9)
Is all about structs, the basics can be quite simple
Creationstruct { var name: String }
Computed properties
Just like clases we can create properties that are based on other properties
Property Observers
We can trigger events after and before property value changes with the keywords didSet
and willSet
Lazy Properties
When initializing a struct we most likely have to give it a value right away, that can be delay by using the lazy keyword beforehand, beware that loading dependencies lazyly always comes at an added cost in the future.
Miscelaneos
Structs can have methods, if you want to modify a property inside a struct method you have to use the keyword mutating at the method level, and lastly strings are structs, explanation as of way can be lengthy.
Structs require initializers by default, so creating one with init()
and add no params to it let us add default values to our properties.
To reference properties inside the Struct you can call them by using self.propertyName
Like in any other language adding the keyword static
before a property makes them accessible with StructName.PropertyName
.
Lastly, structs can have access modifiers, I wonder what is the difference between these and classes? Guess I'll find out next module
Original Link: https://dev.to/davjvo/100-days-of-swift-day-8-and-9-1fbi
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To