Firestore 自定义文档数据映射
Firestore custom document data mapping
Firestore Model
我在 Firestore 中有以下数据结构。我很难将它映射到我的 ios 应用程序。我正在使用 Swift。任何人都可以提供可编码结构或其他东西吗?
struct CartModel: Codable {
let cart: [CartItemModel]
enum CodingKeys: String, CodingKey {
case cart
}
}
extension CartModel{
static var dummyCartData : CartModel {
.init(cart: [CartItemModel.dummyCartData])
}
}
struct CartItemModel: Codable {
let brand: String
let itemDescription: String
let image: String
let selection: [UserSelection]
let title:String
enum CodingKeys: String, CodingKey {
case brand
case itemDescription = "description"
case image
case selection
case title
}
}
extension CartItemModel{
static var dummyCartData : CartItemModel {
.init(brand: "gucci",
itemDescription: "adidas",
image: "yeezy",
selection: [UserSelection.dummyData],
title: "123")
}
}
struct UserSelection: Codable, Equatable{
var size :String
var count : Int
enum CodingKeys: String, CodingKey {
case size = "size"
case count
}
}
extension UserSelection{
static var dummyData : UserSelection {
.init(size: "XS11", count: 20)
}
}
docRef.getDocument { (snapshot, error) in
if error != nil {
return
}
if let snapshot = snapshot, snapshot.exists {
print(snapshot.data())
let products = try! snapshot.data(as: CartModel.self)
print(products)
}
}
Firestore returns
Optional(["cart": <__NSArrayM 0x600002979380>(
{
brand = Nike;
description = "Find the Jordan Point Lane at . ";
image = "https://static.nike.com/a/images/t_default/f105dd55-53d0-4a50-b43a-55bd9231c658/jordan-point-lane-mens-shoes-PPMHdC.png";
selection = {
count = 1;
size = "";
};
title = "Jordan Point Lane Men's Shoes";
}
)
])
致命错误:'try!' 表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "cart", intValue:无),
_FirestoreKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "selection", intValue: nil)], debugDescription: "Not an array", underlyingError: nil))
致命错误:'try!' 表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "cart", intValue: nil), _FirestoreKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "selection", intValue: nil)], debugDescription: "Not an array", underlyingError: nil))
如错误所述,selection
不是 Firestore 文档中的数组,但您已将其定义为模型中的数组。
将let selection: [UserSelection]
更改为let selection: UserSelection
Firestore Model
我在 Firestore 中有以下数据结构。我很难将它映射到我的 ios 应用程序。我正在使用 Swift。任何人都可以提供可编码结构或其他东西吗?
struct CartModel: Codable {
let cart: [CartItemModel]
enum CodingKeys: String, CodingKey {
case cart
}
}
extension CartModel{
static var dummyCartData : CartModel {
.init(cart: [CartItemModel.dummyCartData])
}
}
struct CartItemModel: Codable {
let brand: String
let itemDescription: String
let image: String
let selection: [UserSelection]
let title:String
enum CodingKeys: String, CodingKey {
case brand
case itemDescription = "description"
case image
case selection
case title
}
}
extension CartItemModel{
static var dummyCartData : CartItemModel {
.init(brand: "gucci",
itemDescription: "adidas",
image: "yeezy",
selection: [UserSelection.dummyData],
title: "123")
}
}
struct UserSelection: Codable, Equatable{
var size :String
var count : Int
enum CodingKeys: String, CodingKey {
case size = "size"
case count
}
}
extension UserSelection{
static var dummyData : UserSelection {
.init(size: "XS11", count: 20)
}
}
docRef.getDocument { (snapshot, error) in
if error != nil {
return
}
if let snapshot = snapshot, snapshot.exists {
print(snapshot.data())
let products = try! snapshot.data(as: CartModel.self)
print(products)
}
}
Firestore returns
Optional(["cart": <__NSArrayM 0x600002979380>(
{
brand = Nike;
description = "Find the Jordan Point Lane at . ";
image = "https://static.nike.com/a/images/t_default/f105dd55-53d0-4a50-b43a-55bd9231c658/jordan-point-lane-mens-shoes-PPMHdC.png";
selection = {
count = 1;
size = "";
};
title = "Jordan Point Lane Men's Shoes";
}
)
])
致命错误:'try!' 表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "cart", intValue:无),
_FirestoreKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "selection", intValue: nil)], debugDescription: "Not an array", underlyingError: nil))
致命错误:'try!' 表达式意外引发错误:Swift.DecodingError.typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "cart", intValue: nil), _FirestoreKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "selection", intValue: nil)], debugDescription: "Not an array", underlyingError: nil))
如错误所述,selection
不是 Firestore 文档中的数组,但您已将其定义为模型中的数组。
将let selection: [UserSelection]
更改为let selection: UserSelection