SwiftUI tutorial : complex ui

Hi team, I’m working (slowly 😂) through the SwiftUI tutorial (creating a landmarks app) but have run into a bug I can’t figure out.
I modified the app to retrieve its json data from a remote web server which has worked fine right up to adding the featured section where it creates an index out of bounds error.
it seems like the features array isn’t getting populated but can’t see how to resolve it.

any ideas?

Code Block
import Foundation
import Combine
import SwiftUI
class ModelData: ObservableObject {
@Published var landmarks: [Landmark] = []
var cancellationToken: AnyCancellable?
init(){
getLocations()
}
var categories: [String: [Landmark]] {
Dictionary(
grouping: landmarks,
by: { $0.category.rawValue }
)
}
var features: [Landmark] {
landmarks.filter { $0.isFeatured }
}
}
extension ModelData {
func getLocations() {
let url = "https://overseer.cyou/heritage/theList.php?token=" + randomString(length: 6)
cancellationToken = apiRequest.request(url)?
.mapError({ (error) -> Error in
return error
})
.sink(receiveCompletion: { _ in },
receiveValue: {
self.landmarks = $0
})
}
func randomString(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
}
struct apiRequest {
static func request(_ path: String) -> AnyPublisher<[Landmark], Error>? {
guard let url = URL(string: path)
else { return nil }
let request = URLRequest(url: url)
return apiCall.run(request)
.map(\.value)
.eraseToAnyPublisher()
}
}
struct apiCall {
struct Response<T> {
let value: T
let response: URLResponse
}
static func run<T: Decodable>(_ request: URLRequest) -> AnyPublisher<Response<T>, Error> {
return URLSession.shared
.dataTaskPublisher(for: request)
.tryMap { result -> Response<T> in
let value = try JSONDecoder().decode(T.self, from: result.data)
print(result.response)
print(value)
return Response(value: value, response: result.response)
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}


SwiftUI tutorial : complex ui
 
 
Q