SwiftUI:ForEach过滤器布尔值 [英] SwiftUI: ForEach filters boolean

查看:71
本文介绍了SwiftUI:ForEach过滤器布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼命尝试与ForEach仅显示具有true值的内容.不管我尝试什么,都会显示false.现在空气已经散尽,我认为它是如此简单,以至于我根本看不到它.这是应应用的一段代码:

I'm trying desperately with ForEach to only display what has the value true. No matter what I try, false is also displayed. The air is out now and I think it's so simple that I just don't see it. Here is a piece of code to which it should apply:

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)")
     }
}

这篇关于SwiftUI:ForEach过滤器布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆