gpt4 book ai didi

foreach - SwiftUI:ForEach 过滤器 bool 值

转载 作者:行者123 更新时间:2023-12-02 16:47:08 25 4
gpt4 key购买 nike

我拼命尝试使用 ForEach 只显示值为 true 的内容。无论我尝试什么,都会显示 false。现在空气出来了,我认为它是如此简单,以至于我看不到它。这是它应该适用的一段代码:

import SwiftUI

struct txt: Identifiable {
var id: Int
var text: String
var show: Bool
}

struct ContentView: View {

@State private var array = [
txt(id: 000, text: "True", show: true),
txt(id: 001, text: "True", show: true),
txt(id: 002, text: "True", show: true),
txt(id: 003, text: "False", show: false),
]

var body: some View {
NavigationView {
List {
ForEach(array.indices, id: \.self) { idx in
Text("\(self.array[idx].text)")
}

}
}
}
}

最佳答案

像这样更改您的 ForEach:

ForEach(array) { arr in
if arr.show {
Text("\(arr.text)")
}
}

关于foreach - SwiftUI:ForEach 过滤器 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60138431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com