微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自 watchOS

如何解决来自 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 的更新确实修复了错误是合理的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。