2

I'm getting the following error when trying to follow this tutorial:

Oh no something went wrong: 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.

when using the following code to call 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)")
        }
    }
}

My Thing model is set as so: enter image description here

and i currently have two Things added: enter image description here

My Thing class looks like so:

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

Can anyone see what im missing?

Wazza
  • 1,725
  • 2
  • 17
  • 49

2 Answers2

5

so Contentful's documentation is all over the place. I had the same issue, but I managed to solve it after checking their documentation in their GitHub repo itself.

Basically you need to pass all Swift classes that conform the 'EntryDecodable' and 'FieldKeysQueryable' inside the Client initializer method.

Hope it helps!

  • 1
    This is amazing thank you! For anyone else reading this `contentTypeClasses` must be passed as shown in the Readme pointed out by @Gabriel Trujillo here: https://github.com/contentful/contentful.swift#map-contentful-entries-to-swift-classes-via-entrydecodable – Wazza Jul 12 '19 at 13:56
  • Thank you. I read it 5 times in the console but it managed to go around my head :) – nivbp Sep 09 '20 at 11:45
0

Just wanted to make it easier for people visiting. All you need to do is ensure you've passed in your contentTypeClasses into the client Init method.

https://github.com/contentful/contentful.swift#map-contentful-entries-to-swift-classes-via-entrydecodable

fileprivate let client = Client(spaceId: spaceKey, accessToken: contentDeliveryKey, contentTypeClasses: [YourCustomClass.self])
final class YourCustomClass: EntryDecodable, FieldKeysQueryable {
    static let contentTypeId: ContentTypeId = "yourCustomContentfulType"

    public required init(from decoder: Decoder) throws {
        let sys         = try decoder.sys()
        id              = sys.id
        localeCode      = sys.locale
        updatedAt       = sys.updatedAt
        createdAt       = sys.createdAt
}

Thanks @Wazza for sharing the link to contentful's github docs.

Austin Betzer
  • 72
  • 1
  • 5