我有一個回傳專案陣列的 Google Firebase 云函式。在我的 iOS 應用程式中,我需要獲取回傳的陣列并將其附加到我的 Swift 代碼中的另一個陣列中。
到目前為止,我在 Swift 中得到了以下資訊:
struct Item: Identifiable {
let id: UUID
let user: String
let name: String
let icon: String
let latitude: Double
let longitude: Double
init(id: UUID = UUID(), user: String, name: String, icon: String, latitude: Double, longitude: Double) {
self.id = id
self.user = user
self.name = name
self.icon = icon
self.latitude = latitude
self.longitude = longitude
}
}
@State var items = [Item]() // This is the array that I need to put the returned data into.
Functions.functions().httpsCallable("getItems").call() { result, error in
// This is where I'm stuck. I need to put the items from the result into the items array.
}
這就是結果?.data 等于:
Optional({
items = (
{
icon = snow;
latitude = "39.13113";
longitude = "-84.518387";
name = Clothing;
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
},
{
icon = eyeglasses;
latitude = "37.785834";
longitude = "-122.406417";
name = Glasses;
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
},
{
icon = "wallet.pass";
latitude = "37.785834";
longitude = "-122.406417";
name = Wallet;
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
},
{
icon = key;
latitude = "37.785834";
longitude = "-122.406417";
name = Keys;
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
},
{
icon = laptopcomputer;
latitude = "37.785834";
longitude = "-122.406417";
name = "Laptop/Tablet";
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
},
{
icon = iphone;
latitude = "37.785834";
longitude = "-122.406417";
name = Phone;
user = tS7T8ATGCLTZOXi3ZGr0iaWWJAf1;
}
);
})
火力基地功能:
exports.getItems = functions.https.onCall(async (data, context) => {
let resultMessage;
let items = [];
if (context.auth.uid) {
const itemsRef = db.collection('items');
const itemsRes = await itemsRef.orderBy('timestamp', 'desc').limit(255).get();
if (!itemsRes.empty) {
itemsRes.forEach(itemDoc => {
const item = {
user: itemDoc.data().user,
name: itemDoc.data().name,
icon: itemDoc.data().icon,
latitude: itemDoc.data().latitude,
longitude: itemDoc.data().longitude
}
items.push(item);
});
resultMessage = "Successfully got the items.";
} else {
resultMessage = "Failed to get the items because there are no items to get.";
}
} else {
resultMessage = "Failed to get the items because the user does not have an ID.";
}
functions.logger.log(resultMessage);
return {
items: items
};
});
我覺得這應該非常簡單,但對 Swift 來說還是很陌生。我閱讀了 Google Firebase 的檔案并弄清楚了如何獲取單個變數而不是陣列。我也一直在搜索互聯網,但沒有找到解決方案。任何幫助將不勝感激!!
uj5u.com熱心網友回復:
要直接回答您的問題,您的云函式將回傳一個本機 JavaScript 物件,您需要將其正確轉換為本機 Swift 物件。您的云函式實際上回傳了一個“字典”(Object在 JavaScript 中),它只包含一個陣列,所以我會放棄這種模式并只回傳陣列(否則你必須在客戶端上再次解壓它,這是多余的)。因此,在您的云函式中,只需回傳陣列:
return items;
這個陣列將NSArray在 Swift 客戶端的結果data屬性中打包為一個。因此,只需將其轉換為這樣。然后將該陣列中的每個專案打包為NSDictionary您可以將其視為 Swift[String: Any]字典。
Functions.functions().httpsCallable("getItems").call() { result, error in
// Cast your data as an array of Any
guard let items = result?.data as? [Any] else {
if let error = error {
print(error)
}
return // forgot to add this!
}
// Iterate through your array and cast each iteration as a [String: Any] dictionary
for item in items {
if let item = item as? [String: Any],
let name = item["name"] as? String,
let user = item["user"] as? String,
let icon = item["icon"] as? String,
let latitude = item["latitude"] as? Double,
let longitude = item["longitude"] as? Double {
print(name)
let item = Item(name: name...) // instantiate
items.append(item) // append
}
}
}
決議這些資料有很多更有吸引力的方法。你可以映射陣列,在回圈中集成型別轉換,使用 Firestore 的 SwiftUI API,這只是幾行代碼,等等。
但是,如果您堅持從云函式回傳“字典”,那么只需修改客戶端上的保護陳述句并適當地轉換它:
guard let items = result?.data as? [String: Any] else {
return
}
然后items從字典中獲取型別的值,[Any]然后您就可以參加比賽了。
最后一件事,在服務器端,我會考慮使用try-catch塊來處理錯誤,因為您已經在使用該async-await模式,我認為這是一個好主意。請記住,這些云函式必須通過回傳Promise或拋出特定于 Firebase 的 HTTPS 錯誤來終止。您不會在云功能中進行任何我認為有問題的錯誤處理。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443231.html
上一篇:php中帶條件的陣列總和
下一篇:合并2個陣列而不覆寫
