将对象详细信息传递给另一个视图 [SwiftUI]

Pass Object details to another view [SwiftUI]

面对理解如何使用 NavigationLink 将对象详细信息移动到另一个视图的问题,我阅读了很多文章并观看了视频,除了 Preview 结构外,它们都和我做的一样,它们使用本地数据并且很容易将视图映射到数据的第一项,如 data[0]。在我的例子中,我在线获取数据,因此上述方法并没有帮助我解决预览结构的问题,错误:参数

缺少参数

已阅读文章: developer.apple.com/tutorials/swiftui/building-lists-and-navigation

www.raywenderlich.com/5824937-swiftui-tutorial-navigation

www.youtube.com/watch?v=BCSP_uh0co0&ab_channel=azamsharp

/// 主视图代码:

    import SwiftUI
    import SDWebImageSwiftUI

    struct HomeView: View {
        @State var posts: [Posts] = []
        @State var intPageNo: Int = 0
        var body: some View {
            NavigationView {
                List(posts) {post in
                    NavigationLink(destination: ViewPostView(post: post)){
                        VStack{
                            HStack{
                                WebImage(url: URL(string: post.featured_image))
                                    .resizable()
                                    .placeholder(Image("logo"))
                                    .frame(width: 150, height: 150, alignment: .center)

                                VStack(alignment: .leading, spacing: 10.0){
                                    Text("By: \(post.author_name)")
                                    Text("Since: \(post.since)")
                                    Text("City: \(post.city_name)")
                                    Text("Category: \(post.category_name)")

                                }
                                .font(.footnote)

                                Spacer()
                            }

                            Text(post.title)
                                .font(.body)
                                .fontWeight(.bold)
                                .frame(alignment: .trailing)
                                .flipsForRightToLeftLayoutDirection(true)
                        }
                    }

                }
                .onAppear{
                    self.intPageNo += 1
                    ApiPosts().getPosts(intPage: self.intPageNo){(posts) in
                        self.posts = posts
                    }
                }

                .navigationBarTitle(Text("Home"))
            }

        }
    }

    struct HomeView_Previews: PreviewProvider {
        static var previews: some View {
            HomeView()
        }
    }

/// 详情查看代码:

    import SwiftUI

    struct ViewPostView: View {

        @State var comments: [Comments] = []
        @State var post: Posts

        var body: some View {
            NavigationView {
                VStack{
                    Text(post.post_content)
                        .frame(alignment: .trailing)
                    List(comments){comment in
                        VStack(alignment: .trailing, spacing: 10){
                            HStack(spacing: 40){
                                Text(comment.author_name)
                                Text(comment.comment_date)
                            }

                            Text(comment.comment_content)
                        }
                    }
                    .frame(alignment: .trailing)
                    .onAppear {

                        PostViewManager().getComments(intPostID: self.post.id){(comments) in
                            self.comments = comments
                        }
                    }
                }

            }
        }
    }

    struct ViewPostView_Previews: PreviewProvider {
        static var previews: some View {

            ViewPostView()
        }
    }

/// 获取数据代码:

    struct Comments: Codable, Identifiable {
        var id: Int
        var author_name: String
        var comment_content: String
        var comment_date: String
        var comment_date_gmt: String
        var approved: String
    }

    class PostViewManager {


        func getComments(intPostID: Int, completion: @escaping ([Comments]) -> ()){

            guard let url = URL(string: "https://test.matjri.com/wp-json/matjri/v1/comments/\(intPostID)") else {return}

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                let comments = try! JSONDecoder().decode([Comments].self, from: data!)
                DispatchQueue.main.async {
                    completion(comments)
                }
            }
            .resume()
        }

    }

    struct Posts: Codable, Identifiable {
        var id: Int
        var title: String
        var city_id: Int
        var city_name: String
        var category_id: Int
        var category_name: String
        var since: String
        var author_id: String
        var author_name: String
        var post_content: String
        var featured_image: String
    }

    class ApiPosts {

        func getPosts(intPage: Int, completion: @escaping ([Posts]) -> ()){
            guard let url = URL(string: "https://test.matjri.com/wp-json/matjri/v1/posts/0") else {return}

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                let posts = try! JSONDecoder().decode([Posts].self, from: data!)
                DispatchQueue.main.async {
                    completion(posts)
                }
            }
            .resume()
        }
    }

您收到的错误 "Preview struct, ERROR: Missing argument for parameter",通常是因为您没有为预览提供所需的参数。

ViewPostView 期望通过 "var post: Posts",所以在 ViewPostView_Previews 你 需要提供,例如:

    struct ViewPostView_Previews: PreviewProvider {
    static var previews: some View {
        ViewPostView(post: Posts(id: 1, title: "title", ... ))
    }
}