[英]Use of Array in a Struct in Swift
我有以下結構,我在其中使用字符串、Int 和 Bool。 當我從 Firestore 查詢我的產品時,我會填寫這些變量。 現在我不知道如何在我的結構中處理數組:
struct Product {
var price: Int
var name: String
var isActive: Bool
//var categories: how do I call out the array here?
init(
price: Int,
name: String,
isActive: Bool,
//categories: how do I call out the array here?
){
self.price = price
self.name = name
self.isActive = isActive
//self.categories: how do I call out the array here?
}
init(data: [String: Any]){
price = data[DatabaseRef.price] as? Int ?? 0
name = data[DatabaseRef.name] as? String ?? ""
isActive = data[DatabaseRef.isActive] as? Bool ?? false
//categories: how do I call out the array here?
}
static func modelToData(product: Product) -> [String: Any] {
let data : [String: Any] = [
DatabaseRef.price : product.price,
DatabaseRef.name : product.name,
DatabaseRef.isActive : product.isActive,
//categories: how do I call out the array here?
]
return data
}
}
當我從我的數據庫中查詢我的類別時; 它看起來像這樣:
categories = ["Fruits", "Vegetables", "Frozen"]
不知道如何在我在這里提到的結構的每個部分中調用類別。 新手警告!
你可以像這樣聲明一個數組。
var 類別:[String] = []
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.