CPman
CPman

Reputation: 15

Need help getting NavigationLink to work in SwiftUI

I am currently learning Swift and SwiftUI through www.hackingwithswift.com I am currently going through a coding exercise and can't get my ContentView to link to a different SwiftUI view using NavigationLink.

If I replace my destination with some random text, it works. When I try to insert my SwiftUI view as the destination then the code breaks. I have looked at video after video and I can't understand why my code is not working. Below is my code for my ContentView as well as my SwiftUI view (named: UserView.swift). I think it has something to do with the UserView_Previews, but I am not sure. Any help/ideas/suggestions would be greatly appreciated!

ContentView Code

struct ContentView: View {
    @State private var results = [User]()
    
    var body: some View {
        NavigationView {
            List(results, id: \.id) {item in
                NavigationLink(destination: UserView(results: results)) {
                    VStack(alignment: .leading) {
                        Text(item.name)
                            .font(.headline)
                        Text("Age: \(item.age)")
                            .foregroundColor(Color.red)
                    }
                }
            }
        .onAppear(perform: loadData)
        }
    }
    
    func loadData() {
        guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else {
            print("Invalid URL")
            return
        }
        
        let request = URLRequest(url: url)
        
        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                DispatchQueue.main.async {
                    do {
                        self.results = try JSONDecoder().decode([User].self, from: data)
                    } catch {
                        print(error)
                    }
                }
            }
        }.resume()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

UserView.swift

import SwiftUI

struct UserView: View {
    let results: User
    
    var body: some View {
        ScrollView(.vertical) {
            VStack {
                Text(results.email)
                    .font(.headline)
                Text(results.address)
                
            }
        }
    }
}

struct UserView_Previews: PreviewProvider {
    static let results = User(id: "123", isActive: true, name: "Cody", age: 34, company: "Some Company", email: "Some Email", address: "Some Address", about: "About Me", registered: "Yes", tags: ["1", "2"], friends: [User.Friend(id: "456", name: "Some Friend Name")])
    
    static var previews: some View {
        UserView(results: results)
    }
}

UserFile.swift This is to build my User struct

import Foundation

struct User: Codable, Identifiable {
    let id: String
    let isActive: Bool
    let name: String
    let age: Int
    let company: String
    let email: String
    let address: String
    let about: String
    let registered: String
    let tags: [String]
    let friends: [Friend]
    
    struct Friend: Codable {
        let id: String
        let name: String
    }
}

Upvotes: 1

Views: 545

Answers (1)

vacawama
vacawama

Reputation: 154691

Your List is iterating over results which is [User], so item is a User and that should be passed to UserView() which is expecting a single User, not the array of Users.

var body: some View {
    NavigationView {
        List(results, id: \.id) { item in
            NavigationLink(destination: UserView(results: item)) { // here
                VStack(alignment: .leading) {
                    Text(item.name)
                        .font(.headline)
                    Text("Age: \(item.age)")
                        .foregroundColor(Color.red)
                }
            }
        }
    .onAppear(perform: loadData)
    }
}

I think it has something to do with the UserView_Previews, but I am not sure.

The User_Previews is only unit test data that allows you to test your User View in the Preview Pane in Xcode. It isn't used when you build.

Upvotes: 0

Related Questions