0

The idea from this question answered here Load more functionality using SwiftUI does not include the option if you are getting your ForEach data from a json data source. With the code below what is the best way to achieve the required result?

Group {

    HStack {
        Text("Recommended Events")
           .font(.title3)
           .foregroundColor(.white)
           .fontWeight(.bold)
                                    
          Spacer()
                                    
           Button(action: {
            
           }) {
             Text("Show all")
               .font(.title3)
               .foregroundColor(Color.white)
               .fontWeight(.bold)
               }
           }
            .padding(15)

    ForEach(recommendeds) { recommended in
                                    NavigationLink(destination: RecommendedEventsDetailView(recommended: recommended)) {
                                    RecommendedEventsView(recommended: recommended)
                                    }
                                }

}
EMPIRE
  • 57
  • 7
  • The [Apple SwiftUI Tutorials](https://developer.apple.com/tutorials/swiftui) have examples within them on how to do this. If you download the sample code for the larger projects you will find examples – lorem ipsum Jun 24 '21 at 23:07
  • I went through the tutorials it is still different because it is only for toggle not a load more button. I tried this https://stackoverflow.com/questions/56489712/load-more-functionality-using-swiftui but i am having problems with where to include the range option. ForEach(hotspot) { hotspoties in NavigationLink(destination: HotSpotsDetailView(hotspots: hotspoties)) { HotSpotsItemView(hotspots: hotspoties) } } – EMPIRE Jun 28 '21 at 12:56
  • Range? Nothing about your code talks about range. Are you talking about filtering maybe? – lorem ipsum Jun 28 '21 at 13:02
  • The range i am talking about is in the answer to this question that was asked here https://stackoverflow.com/questions/56489712/load-more-functionality-using-swiftui. I tried using it in my code but could not figure out how to use the range because my data is coming from a local json file. – EMPIRE Jun 30 '21 at 14:16
  • You should look into `.filter` that could be a range of `indices` of your array – lorem ipsum Jun 30 '21 at 14:41

1 Answers1

0

Use the swift Codable protocol. https://www.hackingwithswift.com/example-code/language/how-to-convert-json-into-swift-objects-using-codable You’d have to define a swift struct and decode the json and put the decoded json as the value of an instance of the struct.

zendevil.eth
  • 974
  • 2
  • 9
  • 28
  • I am using the Codable Bundle Extension because i have a lot of json data – EMPIRE Jun 24 '21 at 22:31
  • extension Bundle { func decode(_ file: String) -> T { guard let url = self.url(forResource: file, withExtension: nil) else { fatalError("Failed to locate \(file) in Bundle") } guard let data = try? Data(contentsOf: url) else { fatalError("Failed to load \(file) in Bundle") } let decoder = JSONDecoder() guard let decodedData = try? decoder.decode(T.self, from: data) else { fatalError("Failed to decode \(file) in Bundle") } return decodedData } } – EMPIRE Jun 24 '21 at 22:34