据我所读到的关于条件视图的文章,这段代码应该可以工作。但事实并非如此。
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中找到了一个bug?我上面的代码有用吗?我可以做什么不同的工作,使它与简单的文本清单?
背景:我也有一个TabView和一个不崩溃的if condition { MyTab() }。我有必要担心这可能会以同样的方式崩溃吗?在装船之前,我也应该这样做吗?
PS:我正在使用Xcode 12.4 (12D4e)
发布于 2021-07-28 17:15:21
显然,这是由iOS 15 Beta 4修复的:在我的测试设备上,我使用用Xcode 13 Beta 3编译的测试应用程序,在Beta 4更新之前出现了错误。在iOS 15 Beta 4更新之后,错误就消失了。因此,我认为有理由说iOS 15 Beta 4的更新确实修复了错误。
https://stackoverflow.com/questions/66513742
复制相似问题