Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Runtime Error from if condition { Text() } in watchOS
Hi MDeuschl, Logically, your program should run just fine. I suspect that you are right, that it's a bug with SwiftUI itself. I have the same Fatal error: file SwiftUI, line 0 error for my code. In my case, I conditionally render a view based on whether the variable holding the view is nil. A different situation than yours, but the same error, and for no clear reason. I wish I had more information on how to avoid it. All I know for now is that you can submit a Feedback Assistant - https://feedbackassistant.apple.com/ with a sample Xcode project that demonstrates the crash. I'll probably do this when I have more time. Here's my code. I display my view in another view that is displayed in a modal sheet. The whole modal setup works fine in a SwiftUI Preview, but always crashes on simulator and device. swift import SwiftUI /// A view with an action button that should be presented modally. struct ActionModalView: View {   let iconImage: Image?   let title: String   let message: String   let buttonText: String   let buttonAction: () - Void   var body: some View {     VStack(spacing: Theme.Spacing.extraExtraLarge) {       Spacer(minLength: Theme.Spacing.extraExtraLarge) // This causes a crash       if let iconImage = iconImage {         iconImage           .resizable()           .renderingMode(.template)           .foregroundColor(.primaryBase)           .aspectRatio(contentMode: .fit)           .frame(width: 80)       }       Text(title)         .font(Theme.Text.largeTitle)       FadingScrollView {         Text(message)           .font(Theme.Text.title3)       }       actionButton     }     .padding(.horizontal, Theme.Spacing.extraExtraLarge)     .background(Color.adaptiveSecondaryBackground)   }   var actionButton: some View {     Button(action: buttonAction) {       HStack {         Spacer()         Text(buttonText)           .font(Theme.Text.title3)           .foregroundColor(.textXlight)         Spacer()       }       .padding()       .background(Color.primaryBase)       .cornerRadius(10)     }   } } struct ActionModalView_Previews: PreviewProvider {   static var previews: some View {     LightAndDarkPreviewGroup(layoutMode: .largeAndSmallDevices) {       Group {         Text("Press the play button to view the ActionModalView with a large amount of text.")           .sheet(isPresented: .constant(true)) {             ActionModalView(               iconImage: Image(uiImage: Asset.runningshoe.image),               title: "This is the title",               message: PreviewFixtures.Text.threeParagraphs,               buttonText: "Action Button",               buttonAction: {}             )           }         Text("Press the play button to view the ActionModalView with a small amount of text.")           .sheet(isPresented: .constant(true)) {             ActionModalView(               iconImage: Image(uiImage: Asset.runningshoe.image),               title: "This is the title",               message: PreviewFixtures.Text.oneParagraph,               buttonText: "Action Button",               buttonAction: {}             )           }       }     }   } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to SwiftUI Runtime Error from if condition { Text() } in watchOS
Hi again MDeuschl, I was able to fix the crash in my app. It turns out that in my code that uses my ActionModalView in my previous post, SwiftUI accesses a computed variable, which is generated using the following code: extension String {   var htmlToString: String {     return htmlToAttributedString.string       .trimmingCharacters(in: .whitespacesAndNewlines)   }   private var htmlToAttributedString: NSAttributedString {     guard let data = data(using: .utf8) else { return NSAttributedString() }     do {       return try NSAttributedString(         data: data,         options: [           .documentType: NSAttributedString.DocumentType.html,           .characterEncoding: String.Encoding.utf8.rawValue         ],         documentAttributes: nil)     } catch {       return NSAttributedString()     }   } } It turns out that there's some sort of bug where creating an NSAttributedString with NSAttributedString.DocumentType.html cannot be done on a SwiftUI thread. On iOS 13, it results in a crash, while on iOS 14 it results in a slowdown, and a crash in another part of SwiftUI code. Removing this fixed my issue with my usage of if let iconImage = iconImage in SwiftUI. So it turns out that my usage of if let iconImage = iconImage wasn't really a problem in the first place, and that SwiftUI was doing a really poor job of helping me pinpoint the issue with NSAttributedString. I suspect something similar might be happening in your case. Hopefully SwiftUI improves this year.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21