我正在將 Appwrite 與 Swift 一起使用,并且我正在嘗試使用 convertTo 函式來獲取檔案的 JSON 結構,但我不確定如何繼續。
這是我想使用的功能
public func convertTo<T>(fromJson: ([String: Any]) -> T) -> T {
return fromJson(data)
}
這是我的代碼(我正在嘗試將 documentLits.documents 中的每個檔案轉換為LocationAP結構):
Task {
let documentList = try await AppwriteVM.instance.database.listDocuments(
collectionId: "626b97104b392350d53e"
)
for document in documentList.documents {
let decodedDocument: LocationAP = document.convertTo(fromJson: ?????)
print(decodedDocument.name)
}
}
這是 LocationAP 結構的定義:
struct LocationAP: Codable {
var id: String
var name: String
var latitude: Double
var longitude: Double
}
我的問題是我不確定如何繼續使用document.convertTo(fromJson: <#T##([String : Any]) -> T#>)函式,這是我的代碼的螢屏截圖:
代碼截圖
有誰知道如何進行?
uj5u.com熱心網友回復:
正如 Joakim 所說,您必須撰寫一個包含如何轉換字典的代碼的閉包
let converter : ([String:Any]) -> LocationAP = { dictionary in
// do the conversion and
// return a LocationApi instance
}
for document in documentList.documents {
let decodedDocument = document.convertTo(fromJson: converter)
print(decodedDocument.name)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/468556.html
