watchOS 中 if 条件 { Text() } 的 SwiftUI 运行时错误
SwiftUI Runtime Error from if condition { Text() } in watchOS
据我了解有关条件视图的信息,这段代码应该可以工作。但事实并非如此。
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)
显然这已被 iOS 15 Beta 4 修复:在我的测试设备上,我在 Beta 4 更新之前遇到了错误,使用的是使用 Xcode 13 Beta 3 编译的测试应用程序。更新到 iOS 15 Beta 4 后错误消失了。
所以我认为 iOS 15 Beta 4 的更新确实修复了错误。
据我了解有关条件视图的信息,这段代码应该可以工作。但事实并非如此。
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)
显然这已被 iOS 15 Beta 4 修复:在我的测试设备上,我在 Beta 4 更新之前遇到了错误,使用的是使用 Xcode 13 Beta 3 编译的测试应用程序。更新到 iOS 15 Beta 4 后错误消失了。 所以我认为 iOS 15 Beta 4 的更新确实修复了错误。