我正在尝试将数据保存在Firebase中,但我想按日期保存,以便我可以按日期获取它。
func saveData(){
ref = Database.database().reference()
let userid = Auth.auth().currentUser?.uid
let timestamp = NSDate().timeIntervalSince1970
self.date = timestamp
let fuelDetails:[String:Any] = ["tank_for_refueling" : self.tankForRefuelingTextField.text ?? "",
"fuel_quantity_refueled" : self.fuelQuantityRefueledTextField.text ?? "",
"tank_after_refueling" : self.tankAfterRefuelingTextField.text ?? "",
"date": self.date ?? 0]
ref.child(Constants.NODE_MAINTENANCE).child("date").child(userid!).child(Constants.NODE_FUEL).childByAutoId().setValue(fuelDetails){(error,databaseRef) in
if let error = error{
print(error.localizedDescription)
}
}
}
答案 0 :(得分:0)
您可以使用以下内容:
func saveData(){
// ... other code
let getDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
self.date = formatter.string(from: getDate)
let fuelDetails:[String:Any] = ["tank_for_refueling" : self.tankForRefuelingTextField.text ?? "",
"fuel_quantity_refueled" : self.fuelQuantityRefueledTextField.text ?? "",
"tank_after_refueling" : self.tankAfterRefuelingTextField.text ?? "",
"date": self.date ?? 0]
ref.child(Constants.NODE_MAINTENANCE).child("\(self.date)").child(userid!).child(Constants.NODE_FUEL).childByAutoId().setValue(fuelDetails){(error,databaseRef) in
if let error = error{
print(error.localizedDescription)
}
}
}
编辑 万一Firestore试图解析孩子,语法也会从点更新为破折号