Swiftui (3) 使用Contentful&;从API调用数据时出错;迅捷

Swiftui (3) 使用Contentful&;从API调用数据时出错;迅捷,swiftui,contentful,contentful-api,Swiftui,Contentful,Contentful Api,原谅我,我是一个非常新手与迅捷。。。我正在尝试从CMS中提取数据并将其放入我的应用程序中,但是每次尝试检索和放置数据时都会抛出三个错误 错误在“api.beers.title”、“api.beers.type”和“api.beers.description”部分突出显示 错误 “API”类型的值没有使用根类型“API”的键路径的动态成员“beers” 引用下标“subscript(dynamicMember:)”需要包装器“ObservedObject.wrapper” 初始值设定项“init

原谅我,我是一个非常新手与迅捷。。。我正在尝试从CMS中提取数据并将其放入我的应用程序中,但是每次尝试检索和放置数据时都会抛出三个错误

错误在“api.beers.title”、“api.beers.type”和“api.beers.description”部分突出显示

错误

  • “API”类型的值没有使用根类型“API”的键路径的动态成员“beers”
  • 引用下标“subscript(dynamicMember:)”需要包装器“ObservedObject.wrapper”
  • 初始值设定项“init(:)”要求“Binding”符合“StringProtocol”
API调用代码

func getArray(id: String, completion: @escaping([Entry]) -> ()) {
    let query = Query.where(contentTypeId: id)

    client.fetchArray(of: Entry.self, matching: query) { result in
        switch result {
        case .success(let array):
            DispatchQueue.main.async {
               completion(array.items)
            }
        case .failure(let error):
            print(error)
        }
    }
}

class API: ObservableObject {
    @Published var draft: [Draft] = draftData

    init() {
        getArray(id: "beers") { (items) in
            items.forEach { (item) in
                self.draft.append(Draft(
                    title: item.fields["title"] as! String,
                    type: item.fields["type"] as! String,
                    description: item.fields["type"] as! String
                ))
            }
        }
    }
}
  • 您的草稿是数组,可以通过类似于索引的文本进行访问(api.draft[0].title)
  • @已发布的var草稿:[draft]=draftData而不是@Published var草稿:[draft]=[]
  • 更新:

    class API: ObservableObject {
        @Published var draft: [Draft] = [] 
    
        init() {
            getArray(id: "beers") { (items) in
                items.forEach { (item) in
                    self.draft.append(Draft(
                        title: item.fields["title"] as! String,
                        type: item.fields["type"] as! String,
                        description: item.fields["type"] as! String
                    ))
                }
            }
        }
    }
    
    struct DraftList: View {
        var width: CGFloat = 275
        var height: CGFloat = 200
        @ObservedObject var api = API()
        var body: some View {
                VStack {
                 ForEach(api.draft) {item in 
                    Spacer()
                    Text(item.title)
                        .font(.system(size: 24, weight: .bold))
                        .padding(.horizontal, 20)
                        .frame(width: 275, alignment: .leading)
                        .foregroundColor(Color("TextColor"))
        
                    ...
                 }
                }
                .padding(.horizontal, 20)
                .frame(width: width, height: height)
                .background(Color("TileOrangeColor"))
                .cornerRadius(15)
                .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
            }
        }
    

    因此,如果我不想得到第一个索引为[0],我需要迭代索引是否正确?是的。您需要foreach数组,有点像foreach(draft){item in Text(item.title)…}谢谢。我能得到回报,但它抛出了一个新的错误。。。2020-09-22 22:18:26.945306-0400 PSBC[2691:4738524]致命错误:在展开可选值时意外发现零:file/Users/rob/Desktop/SwiftProjects/PSBC/PSBC/API.swift,第38行,这可能会有所帮助!LOL我的错误类型:item.fields[“type”]as!字符串,我想需要检查的是客户端数组是否有[“type”]字段,或者是否可以这样做:(item[“type”]as!String?)??""
    class API: ObservableObject {
        @Published var draft: [Draft] = [] 
    
        init() {
            getArray(id: "beers") { (items) in
                items.forEach { (item) in
                    self.draft.append(Draft(
                        title: item.fields["title"] as! String,
                        type: item.fields["type"] as! String,
                        description: item.fields["type"] as! String
                    ))
                }
            }
        }
    }
    
    struct DraftList: View {
        var width: CGFloat = 275
        var height: CGFloat = 200
        @ObservedObject var api = API()
        var body: some View {
                VStack {
                 ForEach(api.draft) {item in 
                    Spacer()
                    Text(item.title)
                        .font(.system(size: 24, weight: .bold))
                        .padding(.horizontal, 20)
                        .frame(width: 275, alignment: .leading)
                        .foregroundColor(Color("TextColor"))
        
                    ...
                 }
                }
                .padding(.horizontal, 20)
                .frame(width: width, height: height)
                .background(Color("TileOrangeColor"))
                .cornerRadius(15)
                .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 5)
            }
        }