Skip to content

Instantly share code, notes, and snippets.

@avii-7
Last active September 4, 2024 17:50
Show Gist options
  • Save avii-7/5d5df94ff005f54917ed87b4573ac7cb to your computer and use it in GitHub Desktop.
Save avii-7/5d5df94ff005f54917ed87b4573ac7cb to your computer and use it in GitHub Desktop.
SwiftUI

Property Wrapper

Property Wrappers in Swift allows you to extract common logic in a distinct wrapper object.

When dealing with properties that represent some form of state, it is very common to have some kind of associated logic that gets triggered every time a value is modified. For example, we might validate each new value according to a set of rules, we might transform our assigned value in some way.

For example, let's say that we want to create a property wrapper that automatically capitalizes all String values that were assigned to it. That might be implemeneted like this.

@propertyWrapper
struct Capitalized {
    var wrappedValue: String {
        didSet {
            wrappedValue = wrappedValue.capitalized
        }
    }
    
    init(wrappedValue: String) {
        self.wrappedValue = wrappedValue.capitalized
    }
}

Note how we need to capitalize any string that was passed into our initializer, since property observers are only triggered after a value or object was fully initialized.

It can be implemented using either a struct or class by annotating it with @propertyWrapper attribute. The only requirement is that each property wrapper type should contain a stored property called wrappedValue.


References

  1. https://www.avanderlee.com/swift/property-wrappers/

State Property Wrapper

A property wrapper type that can read and write a value managed by SwiftUI.

Use state as a single source of truth for a given value type that you store in view hierarchy.

Declare state as private to prevent setting it in a memeberwise initializer, which can conflict storage management that SwiftUI provides.

Always initialize state by providing a default value in the state declaration.

SwiftUI manages the property's storage. When the value changes, SwiftUI updates the parts of View hierarchy that depands upon the value.

To access the state underlying value, you use it's wrappedValue property. However, as a shortcut Swift enable you to access the wrapped value by referring directly state instance.

Reference:

  1. https://developer.apple.com/documentation/swiftui/state

StateObject Property Wrapper

Use a state object as a single source of truth for a reference type that you store in a view hierarchy.

Create a state object by applying @StateObject attribute to property declaration and providing an initial value that conforms to the ObservableObject protocol.

When published properties of the observable object change, SwiftUI updates any view depends on those properties.

You can pass a state object into a subview through a property that has @ObservedObject attribute.

Observed objects marked with the @StateObject property wrapper don’t get destroyed and re-instantiated at times their containing view struct redraws. Understanding this difference is essential in cases another view contains your view.

Reference:

  1. https://developer.apple.com/documentation/swiftui/stateobject#overview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment