我必须传递从View接收的movie.id的值,该称为ReviewView。

我需要将在此视图中收到的movie.id值传递给ReviewFetcher,然后使用该movie.id进行网络请求。到目前为止,我已经在ReviewFetcher中对电影ID进行了硬编码,但是我要求从ReviewView那里收到它,然后提出一个请求,然后在ReviewView中更新列表。

以下是代码:

ReviewFetcher.swift

import Foundation
import Alamofire
import SwiftUI

class ReviewObserver: ObservableObject {
    @Published var review = ReviewArray(id: 1, page: 9, results: [])
    // @State var movieID:Int

    init() {
       // self.movieID = movieID
        getReviews(movieID : 181812)
    }

    func getReviews(movieID:Int) {
        //self.review.results.removeAll()
        let reviewURL = "https://api.themoviedb.org/3/movie/"+String(movieID)+"/reviews?api_key=a18f578d774935ef9f0453d7d5fa11ae&language=en-US&page=1"

        Alamofire.request(reviewURL)
            .responseJSON { response in
                if let json = response.result.value {
                    if  (json as? [String : AnyObject]) != nil {
                        if let dictionaryArray = json as? Dictionary<String, AnyObject?> {
                            let json = dictionaryArray
                            if let id = json["id"] as? Int,
                               let page = json["page"] as? Int,
                               let results = json["results"] as? Array<Dictionary<String, AnyObject?>> {
                               for i in 0..<results.count {
                                   if let author = results[i]["author"] as? String,
                                      let content = results[i]["content"] as? String,
                                      let url = results[i]["url"] as? String {

                                       let newReview = ReviewModel(author: author,
                                                                    content: content,
                                                                   url: url)

                                       self.review.results.append(newReview)

                                    }
                                }
                            }
                        }
                    }
                }
            }
       }
  }


ReviewView.swift

import SwiftUI

struct ReviewsView: View {
    @State var movie: MovieModel
    @Binding var reviews:[ReviewModel]
    @ObservedObject var fetcher = ReviewObserver()

    var body: some View {
        VStack(alignment:.leading) {
            Text("Review")
                .font(.largeTitle)
                .bold()
                .foregroundColor(Color.steam_rust)
                .padding(.leading)
            Divider()
            // Text(String(fetcher.movieID))
            List(fetcher.review.results) { item in
                VStack(alignment:.leading) {
                    Text("Written by : "+item.author)
                        .font(.body)
                        .bold()
                        .padding(.bottom)
                    Text(item.content)
                        .font(.body)
                        .lineLimit(.max)
                }
            }
        }
    }
}


MovieModel.swift

import Foundation
import SwiftUI
import Combine

struct MovieArray: Codable {
    var page: Int = 0
    var total_results: Int = 0
    var total_pages: Int = 0
    var results: [MovieModel] = []

}

struct MovieModel: Codable, Identifiable {

    var id : Int
    var original_title: String
    var title: String
    var original_language:String
    var overview: String
    var poster_path: String?
    var backdrop_path: String?
    var popularity: Double
    var vote_average: Double
    var vote_count: Int
    var video: Bool
    var adult: Bool
    var release_date: String?
}

最佳答案

删除您的ReviewObserver类的init()。然后在.onAppearVStack修饰符中调用getReviews方法。您需要什么的想法:

class ReviewObserver: ObservableObject {
    @Published var review = ReviewArray(id: 1, page: 9, results: [])
    func getReviews(movieID:Int) {
//you block,, anything you wanna do with movieID.
//Assume you are going to change 'review' variable

 }
}




struct ReviewsView: View {
    @State var movie:MovieModel
    @Binding var reviews:[ReviewModel]
    @ObservedObject var fetcher = ReviewObserver()

    var body: some View {
        VStack(alignment:.leading){
        Text("Review")
        Divider()
        Text(String(fetcher.movieID))
        List(fetcher.review.results)
            {
                item in
                VStack(alignment:.leading){
                    Text("Written by : "+item.author)
                }
            }.onAppear {
                self.fetcher.getReviews(movieID: movie.id)
           }
        }
      }

10-08 12:13