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: