Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 15, 2022 02:59 am GMT

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

Creation
struct { 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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To