Search code examples
jsonswiftsub-array

Value of type 'PodPost' has no member 'episode'


Also don't understand why the downvotes

So the following code is saying it has no member of episode which is correct because the JSON gets passed to PodEpisode first. Then need to pass it to the last subarray

struct PodPost: Codable {
    var programs: [PodEpisode]
}

struct PodEpisode: Codable {
    var episode : [PodProgram]
}

struct PodProgram: Codable, Identifiable {
    let id = UUID()
    var title : String
    var enclosureurl : String
    var summary : String
}

I am calling it via

func getPodcast(podurl:String,completion: @escaping ([PodProgram]) -> ()) {
     
     
       let podurl = podurl
    
        guard let url = URL(string: "https://api.drn1.com.au/api-access/programs/\(podurl)") else { return }
           
           URLSession.shared.dataTask(with: url) { (data, _, _) in
               // Based on the updated structs, you would no
               // longer be decoding an array
               let podcast = try! JSONDecoder().decode(PodPost.self, from: data!)
            
                
               DispatchQueue.main.async{
                   // The array is stored under programs now
                completion(podcast.episode). // THIS IS THE LINE THAT ERROR SHOWS (Value of type 'PodPost' has no member 'episode' )
               }
               
           }
   }

I have tried the following

adding a

let episode = podcast.programs
then using that to call episode.episode // With no success.

Pic of error

enter image description here


Solution

  • let podcast = try! JSONDecoder().decode(PodPost.self, from: data!)
    

    Would give you an object of PodEpisode with property as programs

    Now in the completion block you are trying to call podcast.programs.episode which isn't possible as its an array

    If you need an episode you would need to do something like this

    let episode = podcast.programs.first!.episode