简体   繁体   English

如何在 Swift 中访问 GitHub API?

[英]How could you access the GitHub API in Swift?

I'd like to make an update detection system in my macOS SwiftUI app by pulling the latest release from GitHub via the API and then comparing the tag.我想通过 API 从 GitHub 中提取最新版本,然后比较标签,从而在我的 macOS SwiftUI 应用程序中创建一个更新检测系统。 How would I go about accessing the API from Swift?我将如何从 Swift 访问 API? I've tried using the methods from here, medium.com , here, swifttom.com and here, steveclarkapps.com but none of them accomplish what I'm trying to do.我已经尝试使用这里、 medium.com这里、 swifttom.com这里 steveclarkapps.com 中的方法,但没有一个能完成我想要做的事情。

For the first method, the code functions with the provided example API, but doesn't work with the GitHub API and it returns this error instead:对于第一种方法,代码使用提供的示例 API 运行,但不适用于 GitHub API,而是返回此错误:

Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

Method 2 suffers the same issue.方法 2 遇到同样的问题。

I couldn't even get enough of method 3's code working to try it.我什至无法获得足够的方法 3 的代码来尝试它。

Here's my adapted code based off of the medium.com method:这是我根据 medium.com 方法改编的代码:

Model.swift

import Foundation

struct TaskEntry: Codable {
    let id: Int
    let tag_name: String
    let name: String
}

ContentView.swift

import SwiftUI

struct ContentView: View {
    @State var results = [TaskEntry]()
        
    var body: some View {
        List(results, id: \.id) { item in
            VStack(alignment: .leading) {
                Text(item.name)
            }
        }.onAppear(perform: loadData)
    }
    func loadData() {
        guard let url = URL(string: "https://api.github.com/repos/NCX-Programming/RNGTool/releases/latest") else {
            print("Invalid URL")
            return
        }
        let request = URLRequest(url: url)

        URLSession.shared.dataTask(with: request) { data, response, error in
            if let data = data {
                /*if*/ let response = try! JSONDecoder().decode([TaskEntry].self, from: data) /*{*/
                    DispatchQueue.main.async {
                        self.results = response
                    }
                    return
                /*}*/
            }
        }.resume()
    }
}

Commented out code and variable names that seem irrelevant are just leftovers.注释掉看起来不相关的代码和变量名只是剩下的。

OS: macOS Big Sur 11.6 Xcode version: 13.0操作系统: macOS Big Sur 11.6 Xcode 版本: 13.0

Open this in your browser:在浏览器中打开它:

https://api.github.com/repos/NCX-Programming/RNGTool/releases/latest https://api.github.com/repos/NCX-Programming/RNGTool/releases/latest

You will notice it is not an array but an object.你会注意到它不是一个数组而是一个对象。 You should be decoding an object like this:你应该像这样解码一个对象:

JSONDecoder().decode(TaskEntry.self, from: data)

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

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