Learn Courses My Dashboard

Unable to display API data

Using what I learned from the concurrency videos, I was trying to practice by using a different API, but when I try it there is nothing there, no error and data displaying. When I try a different API such as one from JSON Placeholder I have no issues. What am I doing wrong with this one API?

Model

struct Park: Codable, Identifiable {

let id, name, entityType: String

let timezone: String?

let schedule: [Schedule]

let parks: [Park]?

}

// MARK: - Schedule

struct Schedule: Codable {

let date: String

let type: TypeEnum

let closingTime: Date

let description: String

let openingTime: Date

}

enum Description: String, Codable {

case earlyEntry

case extendedEvening

case parkHopping

case specialTicketedEvent

}

enum TypeEnum: String, Codable {

case info

case operating

case ticketedEvent

}

ViewModel

class ParkListViewModel: ObservableObject {

@Published var parks: [Park] = []

func fetchParks() {

let apiService = APIService(urlString: "https://api.themeparks.wiki/v1/entity/e957da41-3552-4cf6-b636-5babc5cbc4e5/schedule/")

apiService.getJSON { (result: Result<[Park], APIError>) in

switch result {

case .success(let parks):

DispatchQueue.main.async {

self.parks = parks

}

case .failure(let error):

print(error)

}

}

}

}

View

struct ParkListView: View {

@StateObject var vm = ParkListViewModel(forPreview: false)

var body: some View {

NavigationView {

List {

ForEach(vm.parks) { park in

VStack(alignment: .leading) {

Text(park.name)

.font(.title)

}

}

}

.navigationTitle("Parks")

.listStyle(.plain)

.onAppear {

vm.fetchParks()

}

}

}

}

First things first: When posting code to these forums, please post the code as text rather than an image. This makes it far easier to read (some of us have aging eyes!) and also makes it possible for other posters to copy/paste the code in order to test solutions and such without having to retype all your code from scratch. (For instance, I had to type in the API URL in order to see the JSON you are working with, which has the potential to introduce errors and cause frustration. When asking for help, your goal should be to reduce friction.)

To post your code as text, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you, athough it’s usually better to put the backticks before you paste in your code.


As to your problem, first thing I see is that your structs for decoding the JSON result are not correct.

  1. Schedule has a property scheduleDescription but there is no item by that name in the JSON. It should just be description.
  2. Schedule.scheduleDescription (which we’ve already said should be changed to description) is of type Description. The JSON returned by the API has a string value for the description item. The Codable system has no knowledge of how a JSON string should be turned into a Description; you need to tell it how to do so.
  3. Schedule has a property called type which is of type TypeEnum. The Codable system has no knowledge of how a JSON string should be turned into a TypeEnum; you need to tell it how to do so.

There may very well be more issues, but fix those and then see what else is wrong.

Thank you for the suggestions, have made a couple adjustments and I’ve added the code and removed the screenshots.

I should also note that the API call does not return an array of Park items, but rather a single Park item that contains an array of Parks in its parks property.

So when you do this:

apiService.getJSON { (result: Result<[Park], APIError>)

you are asking for the wrong thing.