Demystify SwiftUI

RSS for tag

Discuss the WWDC21 session Demystify SwiftUI.

View Session

Posts under wwdc21-10022 tag

10 Posts
Sort by:
Post not yet marked as solved
2 Replies
668 Views
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) } ... }
Posted Last updated
.
Post not yet marked as solved
2 Replies
194 Views
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?
Posted
by gdeadtoo.
Last updated
.
Post not yet marked as solved
2 Replies
591 Views
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) ) } }
Posted
by smialek.
Last updated
.
Post not yet marked as solved
0 Replies
291 Views
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.
Posted Last updated
.
Post not yet marked as solved
0 Replies
265 Views
The ios14 to run my app, it is no problem. when run my app on the ios13.7 . The Xcode's console print an ERROR : EXC_BAD_SCCESS I look for the web for this question,but i can't find answer for this question . if you know answer ,write down your answer,please. Thank you!
Posted
by seventeen.
Last updated
.
Post not yet marked as solved
0 Replies
373 Views
I have a current app in the store written in Objective C that works OK, but I want to update it via SwiftUI as a universal app. In my original app, I adjust the display brightness for night time use by setting UIScreen.main.brightness = 0.2 for example. When exiting the application, I return it to its previous value. I have been unable to find out how this can be done in SwiftUI. Anyone know?
Posted Last updated
.