Property Wrapper in SwiftUI - Imtiaz211/interviews GitHub Wiki
What is the structure of a SwiftUI app and how does it differ from a UIKit app?
In a
SwiftUI
app, the main entry point is theSceneDelegate
, which sets up the initialUI
of the app and manages the lifecycle of the app’s scenes. The main difference betweenSwiftUI and UIKit
is thatSwiftUI
uses adeclarative
syntax, meaning that you describe the desired state of the UI rather than manually updating it through code.
@AppStorage
reads and writes values from NSUserDefaults, which will automatically reinvoke your view’s body property when the value changes and will refresh your UI if that key changes.
@Binding
refers to value type data owned by a different view. Changing the binding locally changes the remote data too, a connection between 2 things
@Environment
lets us read data from the system, such as
color scheme
,accessibility
options, andtrait collections
, but you can add your own keys here if you want.
@EnvironmentObject
shared with many views in your app automatically stay updated when that data changes, reads a shared object that we placed into the environment.
@FetchRequest
starts a Core Data fetch request for a particular entity.
@Model
@FocusedBinding
is designed to watch for values in the key window, such as a text field that is currently selected.
@FocusedValue
is a simpler version of @FocusedBinding that doesn’t unwrap the bound value for you.
@GestureState
stores values associated with a gesture that is currently in progress, such as how far you have swiped, except it will be reset to its default value when the gesture stops.
@Namespace
creates an animation namespace to allow matched geometry effects, which can be shared by other views. This owns its data.
@NSApplicationDelegateAdaptor
is used to create and register a class as the app delegate for a macOS app.
@UIApplicationDelegateAdaptor
is used to create and register a class as the app delegate for an iOS
@ObservedObject
refers to an instance of an external class that conforms to the ObservableObject protocol.
@Published
is attached to properties inside an ObservableObject, and tells SwiftUI that it should refresh any views that use this property when it is changed.
@ScaledMetric
reads the
user’s Dynamic Type
setting and scales numbers up or down based on an original value you provide.
@SceneStorage
lets us save and restore small amounts of data for state restoration.
@State
lets us manipulate small amounts of value type data locally to a view.
@StateObject
is used to store new instances of reference type data that conforms to the ObservableObject protocol.
Preferences in SwiftUI
SwiftUI
has the environment concept which we can use to pass data down into a view hierarchy. Parent views share its environment with child views and subscribe to the changes. But sometimes we need to pass data up from child view to the parent view.
ZStack
whatever you put into the ZStack first is drawn first, then subsequent views are layered over it. Body propery The
body property
of anySwiftUI
automatically gets the ability to return different views.
ID in foreach
The
id: \.self
part is required so thatSwiftUI
can identify each element in the array uniquely.