gpt4 book ai didi

ios - 将 SAPOData.EntityValue 数组编码/解码到 CompositeStorage

转载 作者:行者123 更新时间:2023-11-30 11:36:59 27 4
gpt4 key购买 nike

我正在使用适用于 iOS 的 SAP Cloud Platform SDK,试图弄清楚如何将 EntityValue 数组放入 CompositeStorage 中,然后将它们取出。我收到错误:

typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [Foundation.(_PlistKey in _5692656F4C05BA2A580AE9322E9FB0A6)(stringValue: "Index 0", intValue: Optional(0)), Foundation.(_PlistKey in _5692656F4C05BA2A580AE9322E9FB0A6)(stringValue: "Index 0", intValue: Optional(0))], debugDescription: "Expected to decode Dictionary but found __NSCFData instead.", underlyingError: nil))

我正在尝试从此处的 CompositeStore 获取值:

var entities: [ExpensereportType] {
do {
guard let results = try Cache.shared.store.get(Array<ExpensereportType>.self, for: CollectionType.expensereport.rawValue) else {
return []
}
return results
}
catch {
print(error)
return []
}
}

它们在这里被编码:

self.service.fetchReservation(matching: query) { result, error in
guard let result = result else { return completionHandler(error!) }

do {
let encodedResult = try JSONEncoder().encode(result)
try self.store.put(encodedResult, for: CollectionType.reservation.rawValue)
}
catch {
completionHandler(error)
}
completionHandler(nil)
}

有什么想法吗?

最佳答案

这个问题的解决方案很有趣:事实证明 JSONEncoder足够聪明,可以处理 Array<EntityValue> 中的任何一个。按原样,或者对各个项目进行 map 编码。但是,JSONDecoder自动处理顶级数组项,并由您来映射解码这些项。

工作解决方案:

// MARK: - Encode
self.service.fetchReservation(matching: query) { result, error in
guard let result = result else {
completionHandler(error!)
return
}

do {
let encodedResult = try JSONEncoder().encode(result)
// try result.map { try JSONEncoder().encode($0) } ALSO VALID
try self.store.put(encodedResult, for: CollectionType.expensereport.rawValue)
}
//...

// MARK: - Decode `Array<ExpensereportType>`
guard let results = try Cache.shared.store.get(Array<ExpensereportType>.self, for: CollectionType.expensereport.rawValue) else {
return []
}
let decoder = JSONDecoder()
decoder.userInfo.updateValue(myDataService.metadata, forKey: CSDLDocument.csdlInfoKey)
return try results.map { try decoder.decode(ExpensereportType.self, from: $0)}

关于ios - 将 SAPOData.EntityValue 数组编码/解码到 CompositeStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640919/

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