Skip to content

Demystify SwiftUI WWDC21 - Notes

Published:
1 min read

Reference: Demystify SwiftUI WWDC21

When SwiftUI looks at your code, it will look for three things:

Identity

Explicit Identity: In this case, .dogTagID represents the ID of the rescueDog data type which conforms to the Identifiable protocol.

Explicit Identity

Implicit or Structural Identity: If a statement or switch statement in a SwiftUI view’s body (@ViewBuilder) creates an implicit ID based on structural differences (i.e., the position of the view relative to its hierarchy).

Structural Identity

Avoid AnyView

View Lifetime

View Lifetime

View Modifier Identity

With this implementation, when date < .now, a different view modifier identity is created, which is not stable.

Unstable Modifier

A better version would be to tie the condition in the value of opacity, so there is just a single View Modifier, which means better performance, more correct code, and SwiftUI is optimized for this kind of code.

Better Implementation