使用 client.fetchArray 时出现内容错误

Waz*_*zza 2 swift contentful

当我尝试按照本教程进行操作时,出现以下错误:

哦,没有出什么问题:A response for the QueryOn<Thing> did return successfully, but a serious error occurred when decoding the array of Thing. Double check that you are passing Thing.self, and references to all other EntryDecodable classes into the Client initializer.

当使用以下代码调用contentful时:

func fetch() {
    let query = QueryOn<Thing>.where(field: .description, .exists(true))

    client.fetchArray(of: Thing.self, matching: query) { (result: Result<ArrayResponse<Thing>>) in

        switch result {
        case .success(let things):
            guard let firstThing = things.items.first else { return }
            print(firstThing)
        case .error(let error):
            print("Oh no something went wrong: \(error)")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

My Thing 模型设置如下:在此输入图像描述

我目前Things添加了两个:在此输入图像描述

我的 Thing 类看起来像这样:

final class Thing: EntryDecodable, FieldKeysQueryable {

    enum FieldKeys: String, CodingKey {
        case name, description
    }

    static let contentTypeId: String = "thing"

    let id: String
    let localeCode: String?
    let updatedAt: Date?
    let createdAt: Date?

    let name: String
    let description: String

    public required init(from decoder: Decoder) throws {
        let sys = try decoder.sys()

        id = sys.id
        localeCode = sys.locale
        updatedAt = sys.updatedAt
        createdAt = sys.createdAt

        let fields = try decoder.contentfulFieldsContainer(keyedBy: Thing.FieldKeys.self)

        self.name  = try! fields.decodeIfPresent(String.self, forKey: .name)!
        self.description = try! fields.decodeIfPresent(String.self, forKey: .description)!
    }
}
Run Code Online (Sandbox Code Playgroud)

谁能看到我缺少什么吗?

小智 6

所以 Contentful 的文档到处都是。我遇到了同样的问题,但在检查了他们的 GitHub 存储库中的文档后我设法解决了这个问题。

基本上,您需要在客户端初始值设定项方法中传递符合“EntryDecodable”和“FieldKeysQueryable”的所有 Swift 类。

希望能帮助到你!