SwiftUI ForEach in JSON

Hi there,


I followed the SwiftUi Landmarks project from Apple developper.

I'm trying to go further and I have a question.


This is what I'm trying to do:


This is my files:


First I've got a JSON file like this: (I also have the file to decode the JSON, excatly the same as the one from the Landmark project)


[
    {
        "name": "Mochaccino Stout",
        "style": "Milk stout",
        "abv": 5.5,
        "category": "Pilot",
        "id": 1001,
        "isFavorite": true,
        "coordinates": {
            "longitude": -3.1877,
            "latitude": 55.9701
        },
        "imageName": "Mochaccino_Stout",
        "description": "Winner of the Bow Bar’s 2014 Dark Beer Challenge, Mochaccino Stout is a huge-bodied, full-flavoured milk stout, flavoured with coffee roast to our exact specifications, raw and roast organic cocoa nibs and Tahitian vanilla. A rich, comforting pint of luxury."
    },
    {
        "name": "Blønd",
        "style": "Oatmeal pale",
        "abv": 4.0,
        "category": "Pilot",
        "id": 1002,
        "isFavorite": false,
        "coordinates": {
            "longitude": -3.1877,
            "latitude": 55.9701
        },
        "imageName": "Blond",
        "description": "Cloudy like a wheat beer, Blønd is a (relatively) low ABV session pint that packs in all the body, bitterness and tropical fruit flavour of a much bigger beer."
    },
    {
        "name": "Vienna Pale",
        "style": "Vienna lager",
        "abv": 4.6,
        "category": "Pilot",
        "id": 1003,
        "isFavorite": false,
        "coordinates": {
            "longitude": -3.1877,
            "latitude": 55.9701
        },
        "imageName": "Vienna_Pale",
        "description": "Based on the classic Vienna Lager style (though technically an ale), and annoyer of a certain type of beer geek, Vienna Pale is a sweet, malty drinking pint, with plenty of Saaz, Citra and Cascade dry-hopping to keep things interesting."
    },
    {
        "name": "Late Shift",
        "style": "IPA",
        "abv": 6.5,
        "category": "Fierce",
        "id": 1004,
        "isFavorite": false,
        "coordinates": {
            "longitude": -2.2193,
            "latitude": 57.2089
        },
        "imageName": "Late_Shift",
        "description": "Late Shift is a crushable New England style IPA with low bitterness, a full on juicy profile and a lot of haze. We use Azacca and Citra hops for a tropical and citrus hop explosion."
    }
]

Then I have this SWIFT file

import SwiftUI

struct Brewery: Hashable, Codable, Identifiable {
    var id: Int
    var name: String
    fileprivate var imageName: String
    var beers: [Beer]
    var category: Category
   
    enum Category: String, CaseIterable, Codable, Hashable {
        case scottish = "Scottish beer"
        case french = "French beer"
    }
}

extension Brewery {
    var image: Image {
        ImageStore.shared.image(name: imageName)
    }



}

struct Beer: Hashable, Codable, Equatable, Identifiable {
    var id: Int
    var name: String
    fileprivate var imageName: String
    var style: String
    var abv: Float
    var isFavorite: Bool
    var description: String
}

extension Beer {
    var image: Image {
        ImageStore.shared.image(name: imageName)
    }
}


My first frame is like this

import SwiftUI

struct ContentView: View {
   
    var body: some View {
        NavigationView {
            List {
                ForEach(breweryData) { brewery in
                    Text(brewery.name)
                   
                }
            }
        .navigationBarTitle(Text("Brewery"))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


But how can I do a menu with only the beers from a specific brewery (shown on frame 2)?

I don't manage to make a ForEach only in the "beers" from the "brewery" section. So I've got all the beers from the JSON file.


Hope I'm clear 😅

Sorry for the long message..

Let me know if you need more informations.


Thanks for your help

What is breweryData ? Is it the Array from lines 01 to 58 ?

If so, where do you get the brewery of each item ?

You could then filter the list depending on the brewery and use this filteredList in place of breweryData

See example code here:

h ttps://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-a-swiftui-list


If not, what is it ?

Sorry I didn't tell...

breweryData is define at the top of my parsing file like this:


let breweryData: [Brewery] = load("beerData.json")

and after I've got the func load to decode the JSON file.


I will have a look on the link you gave me.


Thanks for your answer

SwiftUI ForEach in JSON
 
 
Q