'PodPost' 类型的值没有成员 'episode'

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

也不明白为什么不赞成

所以下面的代码表示它没有剧集成员,这是正确的,因为 JSON 首先传递给 PodEpisode。然后需要传给最后一个子数组

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
}

我是通过

调用的
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' )
               }
               
           }
   }

我试过以下方法

添加一个

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

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

会给你一个 PodEpisode 的对象 属性 作为 programs

现在在完成块中,您正在尝试调用 podcast.programs.episode,这是不可能的,因为它是一个数组

如果你需要一集,你需要做这样的事情

让剧集 = podcast.programs.first!.episode