Abdullah
Abdullah

Reputation: 672

Pass Object details to another view [SwiftUI]

Facing problem to understand how to move Object details to another view using NavigationLink, I have read many articles and watched video, they all do the same as I do except for the Preview struct, they use local data and easily they map the view to the first item of the data like data[0]. While in my case, I fetch the data online, hence the above way did not help me to overcome the issue with the Preview struct, ERROR: Missing argument for parameter

Articles have been read: 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

/// Main View Code:

    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()
        }
    }

/// Detail View Code:

    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()
        }
    }

/// Fetching data Code:

    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()
        }
    }

Upvotes: 1

Views: 1541

Answers (1)

The error you get "Preview struct, ERROR: Missing argument for parameter", typically is because you did not provide the required parameters to the Preview.

ViewPostView expect to be passed "var post: Posts", so in ViewPostView_Previews you need to provide that, for example:

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

Upvotes: 1

Related Questions