gpt4 book ai didi

swift 4 : Parsing Firebase 4 snapshot to Model

转载 作者:行者123 更新时间:2023-11-30 11:45:01 25 4
gpt4 key购买 nike

我使用的是 Swift 4 和 Firebase 4

我有一个名为 Part 的模型

class Part {
var db_id: String?
var can_8130: Bool?
var description: String?
var has_amm: Bool?
var is_esd: Bool?
var is_hazmat: Bool?
var is_priority: Bool?
var msn: String?
var partNum: String?
var received: String?
var receiver: String?
var tail: String?

init(db_id:String?, can_8130: Bool, description: String?, has_amm: Bool?, is_esd: Bool?, is_hazmat: Bool?, is_priority: Bool?, msn: String?, partNum: String?, received: String?, receiver: String?, tail: String?) {
self.db_id = db_id;
self.can_8130 = can_8130;
self.description = description;
self.has_amm = has_amm;
self.is_esd = is_esd;
self.is_hazmat = is_hazmat;
self.is_priority = is_priority;
self.msn = msn;
self.partNum = partNum;
self.received = received;
self.receiver = receiver;
self.tail = tail;
}
}

在我的 ViewController 中,我有 UITableViewDataSource

@IBOutlet weak var tableView: UITableView!

var partsArray: [Part] = []

在 ViewDidLoad 中,我调用retrieveParts()(来自 Firebase4 实时数据库)

function retrieveParts() {
let rampPartsDB = Database.database().reference().child("ramp-parts")
rampPartsDB.observe(.childAdded) { (snapshot) in
if snapshot.childrenCount > 0 {
//print(snapshot)
if let snapValue = snapshot.value as? [String:Any] {
print(snapValue)
let dbID = snapValue["db_id"] as! String
let can8130 = snapValue["can_8130"] as! Bool
let description = snapValue["description"] as! String
let hasAMM = snapValue["has_amm"] as! Bool
let isESD = snapValue["is_esd"] as! Bool
let isHazmat = snapValue["is_hazmat"] as! Bool
let isPriority = snapValue["is_priority"] as! Bool
let msn = snapValue["msn"] as! String
let partNum = snapValue["partNum"] as! String
let received = snapValue["received"] as! String
let receiver = snapValue["receiver"] as! String
let tail = snapValue["tail"] as! String

var item = Part(db_id: dbID, can_8130: can8130, description: description, has_amm: hasAMM, is_esd: isESD, is_hazmat: isHazmat, is_priority: isPriority, msn: msn, partNum: partNum, received: received, receiver: receiver, tail: tail)
print("ITEM: \(item)" )
self.partsArray.append(item)
}
} // end if
} // end observe()
} // end retrievePart()

现在,“打印零件阵列”显示 []

语句 print(snapValue) 和 print("ITEM: (item)") 为每个成员生成:

["tail": N117UA, "is_priority": 0, "received": 2018-02-22T09:12:07S, "can_8130": 1, "description": PARTS ON A PLANE, "is_hazmat": 0, "is_esd": 1, "has_amm": 0, "partNum": b51-09-1035, "msn": , "receiver": Jon Gravois, "db_id": 99990] ITEM: Aero_Eco.Part

非常感谢任何帮助!

最佳答案

你的快照值是字典里面只需输入此代码if let snapValue = snapshot.value as? [String:Any]' 并在其中 print snapValue 你可以看到所有键值在那里。从中通过键获取每个值并创建模型并附加到您的 partsArray`

在深入了解 Firebase 之前,您需要了解几件事。

Firebase 是一个基于 JSON(键 - 值)的实时数据库类型,允许多个观察者对任意键进行观察。

其中可以有以下事件类型

typedef NS_ENUM(NSInteger, FIRDataEventType) {
/// A new child node is added to a location.
FIRDataEventTypeChildAdded,
/// A child node is removed from a location.
FIRDataEventTypeChildRemoved,
/// A child node at a location changes.
FIRDataEventTypeChildChanged,
/// A child node moves relative to the other child nodes at a location.
FIRDataEventTypeChildMoved,
/// Any data changes at a location or, recursively, at any child node.
FIRDataEventTypeValue
} NS_SWIFT_NAME(DataEventType);

但是还有一件事更重要的是 observeSingleEventOfTypeobserveEventType 我的建议是您只需查看文档 https://firebase.google.com/docs/database/ios/start

如果你观察singleEvent,当这条语句执行时它只会执行一次。如果您观察正常事件,每当您的任何子树发生变化时,它都会通知您。因此建议明智地使用它,不要将观察者放在树对象的顶部。

如果您只需要一次,您应该做什么。

观察按键上的单个事件,然后添加另一个观察者,即 childAdded

关于 swift 4 : Parsing Firebase 4 snapshot to Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48946593/

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