【问题标题】:Show only selection of array in SwiftUI using unique Int使用唯一的 Int 仅显示 SwiftUI 中的数组选择
【发布时间】:2019-07-31 12:57:46
【问题描述】:

1) 我想显示来自多个数组的数据(数据是相同的,只是分成不同的数组)。我该怎么做?

2) 我只想显示整个数组中的特定选择。

我在这些数组中创建了一种 ID 形式,称为“groupid: Int”,它出现在数组中。在这种情况下,我想从多个数组中提取所有带有“groupid:1”的条目并将它们显示在一个列表中。

// 我要在其中显示最终列表的视图:

import SwiftUI

 struct French1View : View {

    var body: some View {
        NavigationView {
            List {
                ForEach(XXXXX????) { grape in
                    GrapeCell(grape: grape)
                }
                .navigationBarTitle("French wine grapes")
            }
    }
    }
}

// 数组:

let BGrapes = [
    Grape(id: 1,
          groupid: 1,
          name: "Barbera",
          type: "White"),
    Grape(id: 2,
          groupid: 2,
          name: "Bosco",
          type: "Red")
 ]


let CGrapes = [
    Grape(id: 1,
          groupid: 1,
          name: "Barbera",
          type: "White")
]

正如您在代码中看到的那样,我被困在我应该输入的内容上(对于这个例子)我写了“XXXXX????”

我试过写“(BGrapes, CGrapes) && grape.groupid(1)”但没有成功。

【问题讨论】:

    标签: arrays swift swiftui


    【解决方案1】:

    欢迎来到 SO。 :)

    您可以将 Grape 数组与 + (Swift 5+) 和 filter groupID 连接起来:

    var body: some View {
        NavigationView {
            List {
                ForEach((bGrapes + cGrapes).filter { $0.groupID == 1 }, id: \.self) { grape in
                    GrapeCell(grape: grape)
                }
                .navigationBarTitle("French wine grapes")
            }
        }
    }
    

    生产:

    如果您希望结果唯一

    var body: some View {
        NavigationView {
            List {
                ForEach(uniqueGrapesFirstGroup(), id: \.self) { grape in
                    GrapeCell(grape: grape)
                }
                .navigationBarTitle("French wine grapes")
            }
        }
    }
    
    func uniqueGrapesFirstGroup() -> [Grape] {
        let allGrapes = (bGrapes + cGrapes)
        let allGrapesFirstGroup = allGrapes.filter { $0.groupID == 1 }
        let allUniqueGrapesFirstGroup = Set(allGrapesFirstGroup)
        return Array(allUniqueGrapesFirstGroup)
    }
    

    生产:

    完整代码:

    struct Grape: Hashable {
        let id: Int
        let groupID: Int
        let name: String
        let type: String
    }
    
    struct GrapeCell: View {
        let grape: Grape
        var body: some View {
            Group {
                Text("Name: \(grape.name)")
            }
        }
    }
    
    struct FrenchView: View {
        let bGrapes = [
            Grape(id: 1,
                  groupID: 1,
                  name: "Barbera",
                  type: "White"),
            Grape(id: 2,
                  groupID: 2,
                  name: "Bosco",
                  type: "Red")
        ]
        let cGrapes = [
            Grape(id: 1,
                  groupID: 1,
                  name: "Barbera",
                  type: "White")
        ]
    
        var body: some View {
            NavigationView {
                List {
                    ForEach(uniqueGrapesFirstGroup(), id: \.self) { grape in
                        GrapeCell(grape: grape)
                    }
                    .navigationBarTitle("French wine grapes")
                }
            }
        }
    
        func uniqueGrapesFirstGroup() -> [Grape] {
            let allGrapes = (bGrapes + cGrapes)
            let allGrapesFirstGroup = allGrapes.filter { $0.groupID == 1 }
            let allUniqueGrapesFirstGroup = Set(allGrapesFirstGroup)
            return Array(allUniqueGrapesFirstGroup)
        }
    }
    

    【讨论】:

    【解决方案2】:

    您可以组合数组并通过 groupid 过滤它们:

    [BGrapes, CGrapes].flatMap { array in
        array.lazy.filter { $0.groupid == 1 }
    }
    

    【讨论】:

    • 可以.lazy内部表达式,防止中间数组分配
    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多