Demystify SwiftUI

RSS for tag

Discuss the WWDC21 session Demystify SwiftUI.

Posts under wwdc21-10022 tag

6 Posts

Post

Replies

Boosts

Views

Activity

Conditionals and Identity
When using conditionals in view bodies, can I preserve identity between the true and false sides of the conditional by adding an explicit id? struct DogTreat: Identifiable { var expirationDate: Date var serialID: String var id: String { serialID } } ... struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { if isExpired { DogTreatView(treat) .id(treat.id) .opacity(0.75) else { DogTreatView(treat) .id(treat.id) } } ... } Does this perform / behave the same as struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { DogTreatView(treat) .opacity(isExpired ? 0.75 : 1.0) } ... }
2
0
1.9k
May ’22
View body is called although its @Binding has not changed
SwiftUI promise is to call View’s body only when needed to avoid invalidating views whose State has not changed. However, there are some cases when this promise is not kept and the View is updated even though its state has not changed. Example: struct InsideView: View { @Binding var value: Int // … } Looking at that view, we’d expect that its body is called when the value changes. However, this is not always true and it depends on how that binding is passed to the view. When the view is created this way, everything works as expected and InsideView is not updated when value hasn’t changed. @State private var value: Int = 0 InsideView(value: $value) In the example below, InsideView will be incorrectly updated even when value has not changed. It will be updated whenever its container is updated too. var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } InsideView(value: customBinding) Can anyone explain this and say whether it's expected? Is there any way to avoid this behaviour that can ultimately lead to performance issues? Here's a sample project if anyone wants to play with it: import SwiftUI struct ContentView: View { @State private var tab = 0 @State private var count = 0 @State private var someValue: Int = 100 var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } var body: some View { VStack { Picker("Tab", selection: $tab) { Text("@Binding from @State").tag(0) Text("Custom @Binding").tag(1) } .pickerStyle(SegmentedPickerStyle()) VStack(spacing: 10) { if tab == 0 { Text("When you tap a button, a view below should not be updated. That's a desired behaviour.") InsideView(value: $someValue) } else if tab == 1 { Text("When you tap a button, a view below will be updated (its background color will be set to random value to indicate this). This is unexpected because the view State has not changed.") InsideView(value: customBinding) } } .frame(width: 250, height: 150) Button("Tap! Count: \(count)") { count += 1 } } .frame(width: 300, height: 350) .padding() } } struct InsideView: View { @Binding var value: Int var body: some View { print("[⚠️] InsideView body.") return VStack { Text("I'm a child view. My body should be called only once.") .multilineTextAlignment(.center) Text("Value: \(value)") } .background(Color.random) } } extension ShapeStyle where Self == Color { static var random: Color { Color( red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1) ) } }
2
0
2.1k
Dec ’21
Swift in XCode
Hi there, I am trying to upload my AR experience from Reality Composer into XCode using Swift. Although I have followed the documentation from Apple's presentation, minute 40:53, (https://developer.apple.com/videos/play/wwdc2019/609/?time=2453) of working with AR and XCode, I receive an error message every time I try to load my scene, as the load prompt seems to become part of the scene title and my title itself is not recognized. For example: Module '...' has no member named 'load...'. If anyone can help me here, I would be very grateful.
0
0
476
Oct ’21
Conditionals and Identity
When using conditionals in view bodies, can I preserve identity between the true and false sides of the conditional by adding an explicit id? struct DogTreat: Identifiable { var expirationDate: Date var serialID: String var id: String { serialID } } ... struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { if isExpired { DogTreatView(treat) .id(treat.id) .opacity(0.75) else { DogTreatView(treat) .id(treat.id) } } ... } Does this perform / behave the same as struct WrapperView: View { ... var treat: DogTreat var isExpired: Bool { treat.expirationDate < .now } var body: some View { DogTreatView(treat) .opacity(isExpired ? 0.75 : 1.0) } ... }
Replies
2
Boosts
0
Views
1.9k
Activity
May ’22
Creating Children Interactive book in Swift UI or Playground
Could you please show me an example of a complete project that shows how to create Children Interactive books in Swift UI or Playground with links to various pages of the book that i can follow step-by-step?
Replies
0
Boosts
0
Views
671
Activity
Mar ’22
LandmarkRow.swift Cannot Preview in This File
I'm following the Swift Tutorial, and everything is working ok until I create the LandmarkRow.swift I keep getting the "Cannot Preview in This File" error, even after copying and pasting the code from the tutorial. Any ideas?
Replies
2
Boosts
0
Views
653
Activity
Feb ’22
SwiftUI ScrollView and LazyVStack issue
Bouncing from top to bottom won’t work properly. It’s working only when load some content more than screen height size… iOS14! But, on iOS 15 all work like created
Replies
0
Boosts
0
Views
1.1k
Activity
Feb ’22
View body is called although its @Binding has not changed
SwiftUI promise is to call View’s body only when needed to avoid invalidating views whose State has not changed. However, there are some cases when this promise is not kept and the View is updated even though its state has not changed. Example: struct InsideView: View { @Binding var value: Int // … } Looking at that view, we’d expect that its body is called when the value changes. However, this is not always true and it depends on how that binding is passed to the view. When the view is created this way, everything works as expected and InsideView is not updated when value hasn’t changed. @State private var value: Int = 0 InsideView(value: $value) In the example below, InsideView will be incorrectly updated even when value has not changed. It will be updated whenever its container is updated too. var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } InsideView(value: customBinding) Can anyone explain this and say whether it's expected? Is there any way to avoid this behaviour that can ultimately lead to performance issues? Here's a sample project if anyone wants to play with it: import SwiftUI struct ContentView: View { @State private var tab = 0 @State private var count = 0 @State private var someValue: Int = 100 var customBinding: Binding<Int> { Binding<Int> { 100 } set: { _ in } } var body: some View { VStack { Picker("Tab", selection: $tab) { Text("@Binding from @State").tag(0) Text("Custom @Binding").tag(1) } .pickerStyle(SegmentedPickerStyle()) VStack(spacing: 10) { if tab == 0 { Text("When you tap a button, a view below should not be updated. That's a desired behaviour.") InsideView(value: $someValue) } else if tab == 1 { Text("When you tap a button, a view below will be updated (its background color will be set to random value to indicate this). This is unexpected because the view State has not changed.") InsideView(value: customBinding) } } .frame(width: 250, height: 150) Button("Tap! Count: \(count)") { count += 1 } } .frame(width: 300, height: 350) .padding() } } struct InsideView: View { @Binding var value: Int var body: some View { print("[⚠️] InsideView body.") return VStack { Text("I'm a child view. My body should be called only once.") .multilineTextAlignment(.center) Text("Value: \(value)") } .background(Color.random) } } extension ShapeStyle where Self == Color { static var random: Color { Color( red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1) ) } }
Replies
2
Boosts
0
Views
2.1k
Activity
Dec ’21
Swift in XCode
Hi there, I am trying to upload my AR experience from Reality Composer into XCode using Swift. Although I have followed the documentation from Apple's presentation, minute 40:53, (https://developer.apple.com/videos/play/wwdc2019/609/?time=2453) of working with AR and XCode, I receive an error message every time I try to load my scene, as the load prompt seems to become part of the scene title and my title itself is not recognized. For example: Module '...' has no member named 'load...'. If anyone can help me here, I would be very grateful.
Replies
0
Boosts
0
Views
476
Activity
Oct ’21