gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:14:39 27 4
gpt4 key购买 nike

我在尝试关注 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.

当使用以下代码调用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 模型设置如下:enter image description here

我目前有两个Things添加:enter image description here

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

有人能看到我缺少什么吗?

最佳答案

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

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

希望对你有帮助!

关于swift - 使用 client.fetchArray 时出现内容错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56526394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com