我正在尝试学习在 swiftUI 中进行 API 调用,我正在遵循下一个教程https://www.youtube.com/watch?v=1en4JyW3XSI但代码给了我一个我找不到的错误的解决方案。
帖子列表.swift
import SwiftUI
struct PostList: View {
@State var posts: [Post] = []
var body: some View {
List(posts) { post in
Text(post.title)
}
.onAppear(){
Api().getPosts { (posts) in
self.posts = posts
}
}
}
}
struct PostList_Previews: PreviewProvider {
static var previews: some View {
PostList()
}
}
Run Code Online (Sandbox Code Playgroud)
数据.swift
import SwiftUI
struct Post: Codable, Identifiable {
var id = UUID()
var title: String
var body: String
}
class Api{
func getPosts(completition: @escaping([Post]) …Run Code Online (Sandbox Code Playgroud)