Jon Trygve Hegnar
Jon Trygve Hegnar

Reputation: 19

Show only selection of array in SwiftUI using unique Int

1) I want to show data from multiple arrays (the data is identically made, just split into different arrays). How do I do this?

2) I want to show only specific selections from across the arrays.

I have created a form of ID in these arrays called "groupid: Int" which appears across the arrays. In this case, I want to extract all the entries with "groupid: 1" from multiple arrays and show them in a list.

// The view that I want to show the final list in:

import SwiftUI

 struct French1View : View {

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

// The arrays:

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

As you can see in the code, I am stuck on what I should enter where I have (for this example) written "XXXXX????"

I have tried writing "(BGrapes, CGrapes) && grape.groupid(1)" but without success.

Upvotes: 0

Views: 689

Answers (2)

backslash-f
backslash-f

Reputation: 8193

Welcome to SO. :)

You could concatenate the Grape arrays with + (Swift 5+) and filter by 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")
        }
    }
}

Produces:

result

If you want results to be UNIQUE:

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

Produces:

resultsUnique

Full code:

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

Upvotes: 2

user28434'mstep
user28434'mstep

Reputation: 6600

You may combine arrays and filter them by groupid:

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

Upvotes: 0

Related Questions