Содержательная ошибка при использовании client.fetchArray - PullRequest
1 голос
/ 10 июня 2019

Я получаю следующую ошибку при попытке выполнить этот учебник:

О, что-то пошло не так: 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)")
        }
    }
}

Моя модель Thing установлена ​​следующим образом: enter image description here

, и у меня в настоящее время добавлено два Things: enter 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 Ответ

1 голос
/ 11 июля 2019

так что документация Contentful повсюду.У меня была та же проблема, но мне удалось решить ее после проверки их документации в их репозитории GitHub .

В основном вам нужно передать все классы Swift, которые соответствуют EntryDecodable и FieldKeysQueryable внутри метода инициализатора клиента.

Надеюсь, это поможет!

...