使用client.fetchArray

时间:2019-06-10 12:24:58

标签: swift contentful

尝试遵循this教程时出现以下错误:

哦,没有错: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.

使用以下代码调用内容丰富的内容时:

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)")
        }
    }
}

我的事物模型设置为:enter image description here

我目前添加了两个Thingsenter image description here

我的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)!
    }
}

谁能看到我想念的东西吗?

1 个答案:

答案 0 :(得分:1)

因此,Contentful的文档无处不在。我遇到了同样的问题,但是在查看他们在GitHub存储库itself中的文档后,我设法解决了这个问题。

基本上,您需要在Client初始化方法中传递所有符合'EntryDecodable'和'FieldKeysQueryable'的Swift类。

希望有帮助!