简体   繁体   English

如何在 SwiftUI 中的两个 API 参数之间导航

[英]How to navigate between two API parameters in SwiftUI

I'm beginner of iOS app development, currently doing iOS & Swift Bootcamp on Udemy by Angela Yu.我是 iOS 应用程序开发的初学者,目前正在由 Angela Yu 在 Udemy 上进行 iOS 和 Swift Bootcamp。 I have this app called H4X0R News, which shows Hacker News all stories that are on the front/home page on the app by using its API .我有一个名为 H4X0R News 的应用程序,它使用API显示Hacker News应用程序首页/主页上的所有故事。 By the end of a module the app works fine when url property from API json is not nil but there are certain cases when url equals nil. By the end of a module the app works fine when url property from API json is not nil but there are certain cases when url equals nil. These are posts which instead has story_text property.这些是具有story_text属性的帖子。 So what I want here to adjust is add story_text to my code and use it to navigate between this and url parameter.所以我想在这里调整的是在我的代码中添加story_text ,并使用它在这个和url参数之间导航。 Here's the code I've got:这是我得到的代码:

ContentView.swift ContentView.swift

import SwiftUI

struct ContentView: View {
    @ObservedObject var networkManager = NetworkManager()
    
    var body: some View {
        NavigationView {
            List(networkManager.posts) { post in
                NavigationLink(destination: DetailView(url: post.url)) {
                    HStack {
                        Text(String(post.points))
                        Text(post.title)
                    }
                }
            }
            .navigationTitle("H4X0R NEWS")
        }
        .onAppear {
            self.networkManager.fechData()
        }
    }
}

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

WebView.swift WebView.swift

import Foundation
import WebKit
import SwiftUI

struct WebView: UIViewRepresentable {
    let  urlString: String?
    
    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        if let safeString = urlString {
            if let url = URL(string: safeString) {
                let request = URLRequest(url: url)
                uiView.load(request)
            }
        }
    }
}

DetailView.swift DetailView.swift

import SwiftUI

struct DetailView: View {
    
    let url: String?
    
    var body: some View {
        WebView(urlString: url)
    }
}

struct DetailView_Previews: PreviewProvider {
    static var previews: some View {
        DetailView(url: "https://www.google.com/")
    }
}

NetworkManager.swift NetworkManager.swift

import Foundation

class NetworkManager: ObservableObject {
    @Published var posts = [Post]()
    
    func fechData() {
        if let url = URL(string: "http://hn.algolia.com/api/v1/search?tags=front_page") {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { data, response, error in
                if error == nil {
                    let decoder = JSONDecoder()
                    if let safeData = data {
                        do {
                            let results = try decoder.decode(Results.self, from: safeData)
                            DispatchQueue.main.async {
                                self.posts = results.hits
                            }
                        } catch {
                            print(error)
                        }
                        
                    }
                }
            }
            task.resume()
        }
    }
}

PostData.swift PostData.swift

import Foundation

struct Results: Decodable {
    let hits: [Post]
}
struct Post: Decodable, Identifiable {
    var id: String {
        return objectID
    }
    let objectID: String
    let points: Int
    let title: String
    let url: String?
}

So what I'm sure I need to add this story_text to PostData as String?那么我确定我需要将此story_text作为String?添加到PostData吗? and then make conditional statement in the WebView.updateUIView() function.然后在WebView.updateUIView() function 中进行条件语句。 Then update the code in other files.然后更新其他文件中的代码。 But like I said, I'm new in the programming world and I seek for help here for the first time since I've started the course.但就像我说的,我是编程世界的新手,自从我开始学习这门课程以来,我第一次在这里寻求帮助。

If I understand you correct, you want to navigate to a simple text view if there is no URL for the Post?如果我的理解正确,如果帖子没有 URL,您想导航到简单的文本视图吗? So you can do it like this:所以你可以这样做:

let text: String?
    
var body: some View {
     if let url = url {
        WebView(urlString: url)
     } else {
        Text(text ?? "-")
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM