TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Project 10, Part 1 - iTunes API returns no content!

Forums > 100 Days of SwiftUI

My code runs and compiles but it doe snot display any content... Any pointers on where to start fault finding The fact that I do not see any content would be much appreciated.

          import SwiftUI

          struct Response: Codable {
              var results: [Result]
          }

          struct Result: Codable {
              var trackID: Int
              var trackName: String
              var collectionName: String
          }

          struct iTunesAPIView: View {
              @State private var results = [Result]()

              var body: some View {
                  List(results, id: \.trackID) { item in
                      VStack(alignment: .leading) {
                          Text(item.trackName)
                              .font(.headline)

                          Text(item.collectionName)
                      }
                  }
                  .task {
                      await loadData()
                  }
              }

              func loadData() async {
                  guard let url = URL(string: "https://itunes.apple.com/serach?term=taylor+swift$identity=song") else {
                      print("Invalid URL")
                      return
                  }

                  do {
                      let (data, _) = try await URLSession.shared.data(from: url)

                      if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data)
                      {
                          results = decodedResponse.results
                      }
                  } catch {
                      print("Invalid Data")
                  }
              }
          }

2      

Check you URL address it is misspelled... In project it is search not serach and entity not identity

URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song")

3      

2      

Unfortunately, even after correct my two spelling mistakes, still no joy...

import SwiftUI

struct Response: Codable { var results: [Result] }

struct Result: Codable { var trackID: Int var trackName: String var collectionName: String }

struct iTunesAPIView: View { @State private var results = [Result]()

var body: some View {
    List(results, id: \.trackID) { item in
        VStack(alignment: .leading) {
            Text(item.trackName)
                .font(.headline)
            Text(item.collectionName)
        }
    }
    .task {
        await loadData()
    }
}

func loadData() async {
    guard let url = URL(string:
        "https://itunes.apple.com/search?term=taylor+swift&entity=song") else {
        print("Invalid URL")
        return
    }

    do {
        let (data, _) = try await URLSession.shared.data(from: url)

        if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
            results = decodedResponse.results
        }
    } catch {
        print("Invalid Data")
    }
}

}

2      

You have to pay attention to data you receive from server. If you go over the lesson again you might notice that

struct Result: Codable {
              var trackId: Int // <-- trackId is spelled this way BUT not trackID
              var trackName: String
              var collectionName: String
          }

you can change it of course but then you have to help Swift to update CodingKeys to decode response. Not sure which lesson covers that. But if you want it to work from the box you HAVE to make sure your property names match those you receive from server.

OR to use CodingKeys like so

struct Result: Codable { 
    var trackID: Int
    var trackName: String
    var collectionName: String

    enum CodingKeys: String, CodingKey {
        case trackID = "trackId"
        case trackName
        case collectionName
    }
}

3      

Thank you, thank you, thank you.

You have taught me that every lower/upper case matters!

It all works now, thank you!

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.