UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.