Knowledgebase

State and Data Flow in SwiftUI Print

  • mobiledevelopment, mobile, themes, guide, howto, solution, zillionkinghost, hosting
  • 0

How data drives the interface.

WHAT THE PROPERTY WRAPPERS DO

Declare how a view relates to a piece of data.

WHAT LOCAL STATE IS FOR

Simple values owned by one view.

WHAT A BINDING IS

A reference to state owned elsewhere, allowing a child to read and write it.

WHAT OBSERVED OBJECTS ARE FOR

Reference types whose changes the view should react to.

WHAT ENVIRONMENT VALUES PROVIDE

Data passed implicitly down the hierarchy, without threading it through every view.

WHEN TO USE THAT

Values many views need: a theme, a session, a formatter.

WHAT THE PRINCIPLE IS

One source of truth for each piece of data.

WHAT VIOLATING IT PRODUCES

Copies that diverge, and interfaces showing inconsistent values.

WHAT TO KEEP LOCAL

Transient interface state.

WHAT TO KEEP IN A MODEL OBJECT

Anything the screen's logic owns.

WHAT TO AVOID

Deriving values and storing them Passing bindings through many levels Heavy work in the view body

WHY THAT LAST POINT

The body may be evaluated frequently.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot