来自 watchOS 中 if 条件 Text() 的 SwiftUI 运行时错误
Posted
技术标签:
【中文标题】来自 watchOS 中 if 条件 Text() 的 SwiftUI 运行时错误【英文标题】:SwiftUI Runtime Error from if condition Text() in watchOS来自 watchOS 中 if 条件 Text() 的 SwiftUI 运行时错误 【发布时间】:2021-03-07 06:12:27 【问题描述】:就我所读到的条件视图而言,这段代码应该可以工作。但事实并非如此。
struct Consts
static let myCondition = false //no difference if true or false
struct ContentView: View
@State var toggle: Bool = false
var body: some View
List()
Text("Short Text is in first line")
Text("Second Line Text is a little longer but not much")
if Consts.myCondition
Text("This is a conditional text. As in: when the user hasn't purchased the option he / she don't need a hint how to use this feature.")
// else
// Text("else doesn't help either.")
Toggle("I also have a toggle but it has nothing to do with this.", isOn: $toggle)
Text("Here we have a longer Text. Dont know what to type any more.")
Text("More text which is longer than a few lines.")
Text("Not so long Text")
.navigationTitle("Hints & Settings")
它编译时没有警告或错误。它在模拟器和设备上加载并显示良好。但是每次我从末尾向上滚动列表时,只要这个if condition Text()
变得可见,应用就会崩溃
Fatal error: file SwiftUI, line 0
2021-03-07 06:36:26.548126+0100 WTFif WatchKit Extension[23163:641812] Fatal error: file SwiftUI, line 0
这不仅限于 watchOS。它在 ios 中以相同的方式重现,只是文本必须更长,以便 if condition Text()
在滚动时变得不可见。
我已经用一个数组、条件范围和两个 ForEach() 块解决了这个错误。
struct ContentView: View
let myHints = ["Short Text is in first line",
"Second Line Text is a little longer but not much",
"This is a conditional text. As in: when the user hasn't purchased the option he / she don't need to hint how to use this feature.",
"Here we have a longer Text. Dont know what to type any more.",
"More text which is longer than a few lines.",
"Not so long Text"]
var myUpperRange: ClosedRange<Int>
if Consts.myCondition
return 0...1
else
return 0...2
var myLowerRange: ClosedRange<Int>
return 3...5
@State var toggle: Bool = false
var body: some View
List()
ForEach (myUpperRange, id: \.self) i in
Text(myHints[i])
Toggle("I also have a toggle but it has nothing to do with this.", isOn: $toggle)
ForEach (myLowerRange, id: \.self) i in
Text(myHints[i])
.navigationTitle("Hints & Settings")
我的问题基本上是:我没有得到它还是我在 Xcode / SwiftUI 中发现了一个错误?我上面的代码应该工作吗?我可以做些什么不同的事情来让它与简单的文本列表一起工作?
背景:我还有一个带有if condition MyTab()
的 TabView,它可以正常工作而不会崩溃。我是否必须担心这可能会以同样的方式崩溃?我是否也应该在发货前解决这个问题?
PS:我使用的是 Xcode 12.4 (12D4e)
【问题讨论】:
【参考方案1】:显然,iOS 15 Beta 4 已解决此问题:在我的测试设备上,使用 Xcode 13 Beta 3 编译的测试应用程序在 Beta 4 更新之前出现错误。更新到 iOS 15 Beta 4 后出现错误离开了。 所以我认为有理由说 iOS 15 Beta 4 的更新确实修复了这个错误。
【讨论】:
以上是关于来自 watchOS 中 if 条件 Text() 的 SwiftUI 运行时错误的主要内容,如果未能解决你的问题,请参考以下文章