Swift 尝试将对象添加到类并将信息保存到Firebase数据库
我想通过应用程序中的视图控制器保存新对象。但是,我希望在应用程序登录时加载这些新对象。我正在使用firebase将数据保存到数据库中,但如何保存对象并在应用程序再次登录时使其返回?我是编程新手,对于可能出现的任何混乱,我深表歉意 这里是应用程序登录后读取目标信息的位置Swift 尝试将对象添加到类并将信息保存到Firebase数据库,swift,firebase,class,firebase-realtime-database,Swift,Firebase,Class,Firebase Realtime Database,我想通过应用程序中的视图控制器保存新对象。但是,我希望在应用程序登录时加载这些新对象。我正在使用firebase将数据保存到数据库中,但如何保存对象并在应用程序再次登录时使其返回?我是编程新手,对于可能出现的任何混乱,我深表歉意 这里是应用程序登录后读取目标信息的位置 for i in 0 ... clientList.count - 1 { screenHandle = ref?.child(organizationCode).child(clientList[i].name).observ
for i in 0 ... clientList.count - 1 {
screenHandle = ref?.child(organizationCode).child(clientList[i].name).observe(.value, with: { (snapshot) in
let clientStuffLoad = snapshot.value as! [String:Any]
if clientStuffLoad["Goal 1 Description"] != nil {
clientList[i].goal1 = clientStuffLoad["Goal 1"] as! String
} else {
clientList[i].goal1 = ""
}
这基本上就是关于向类客户端添加新成员的内容:
@IBAction func addingClientSaveButton(_ sender: Any) {
var client7 = Client(name: addingClientName.text!,
goal1: addingClientGoal1.text!, goal2:
addingClientGoal2.text!,
goal3: addingClientGoal3.text!,
isSelected: false, s: 1,
ind: 1, targetBehavior1 : addingClientTB1.text!,
targetBehavior2 : addingClientTB2.text!,
targetBehavior3 : addingClientTB3.text!,
targetBehavior1Info : addingClientTB1Info.text!,
targetBehavior2Info : addingClientTB2Info.text!,
targetBehavior3Info : addingClientTB3Info.text!)
但是我希望对象名读取客户端名称输入,而不是client7
第二部分是,我想要一种将其写入数据库的方法,并且能够在登录时读取它,这样我就可以使用类的属性,并在添加新客户端时添加到该类中。这是一个非常广泛的问题,因为它涵盖了使用Firebase的许多不同方面;书写、阅读、处理DataSnapshots等。此外,我不知道您的数据代表什么,因此我为自己挑选了一些内容,介绍与Firebase合作的一些方面
没有错误检查,但它按原样工作。我一路上都发表了评论
Firebase没有任何对象;只有父节点和子节点。一切都可以像字典一样被视为键:值对。不能写入对象或读取对象。只有NSString、NSNumber、NSDictionary和可怕的NSArray(或其快速对应项)
让我们从一个类开始——有100种方法可以做到这一点,但我喜欢类负责它们的属性,并接受它们并展示它们
class WineClass {
var wine_key = ""
var name = ""
var varietal = ""
//this is used when creating a new wine object before storing in firebase
init(withName: String, andVarietal: String) {
self.name = withName
self.varietal = andVarietal
}
//this is used when we are loading data from firebase to create the wineclass object
init(withSnapshot: DataSnapshot) {
let wineName = withSnapshot.childSnapshot(forPath: "wine_name").value as? String ?? "No Wine Name"
let wineDict = withSnapshot.value as! [String: Any]
let wineVarietal = wineDict["wine_varietal"] as? String ?? "No Wine Varietal"
self.wine_key = withSnapshot.key //when we read a wine, this will be it's reference in case we want to update or delete it
self.name = wineName
self.varietal = wineVarietal
}
//this is use to create a dictionary of key:value pairs to be written to firebase
func getWineDictForFirebase() -> [String: Any] {
let d = [
"wine_name": self.name,
"wine_varietal": self.varietal
]
return d
}
}
然后,我们需要一个类var来存储wine类的。例如,这将是tableView的数据源
var wineArray = [WineClass]() //a class var array to store my wines
然后我会给你们两个按钮,一个用来填充并向Firebase写入一些葡萄酒,另一个用来读入并打印到控制台
func button0() {
self.writeWine(withName: "Scarecrow", andVarietal: "Red Blend")
self.writeWine(withName: "Ghost Horse", andVarietal: "Cabernet Sauvignon")
self.writeWine(withName: "Screaming Eagle", andVarietal: "Cabernet Sauvignon, Merlot, Cabernet Franc")
}
func button1() {
self.readWines()
}
然后函数接受一些字符串作为每种葡萄酒的属性,并将它们写入Firebase
func writeWine(withName: String, andVarietal: String) {
let newWine = WineClass(withName: withName, andVarietal: andVarietal) //create a new wine object
let wineListRef = self.ref.child("wine_list") //get a reference to my firebase wine_list
let thisWineRef = wineListRef.childByAutoId() //a new node for this wine
let d = newWine.getWineDictForFirebase() //get the wine properties as a dictionary
thisWineRef.setValue(d) //save it in firebase
}
最后是一个函数,它读取这些葡萄酒,并在控制台中打印它们的属性
func readWines() {
let wineRef = self.ref.child("wine_list")
wineRef.observeSingleEvent(of: .value, with: { snapshot in //we are reading in the entire wine node which will contain many child nodes
let allWines = snapshot.children.allObjects as! [DataSnapshot] //cast each child node as a DataSnapshot & store in array
for wineSnap in allWines { //iterate over each child node in the array
let wine = WineClass(withSnapshot: wineSnap) //create a new wine, ensuring we also keep track of it's key
self.wineArray.append(wine) //add to the array
}
for wine in self.wineArray {
print(wine.wine_key, wine.name, wine.varietal)
}
})
}
最后,当点击按钮0时,我们的Firebase如下所示
wine_list
-LhbjhkEC8o9TUISCjdw
wine_name: "Scarecrow"
wine_varietal: "Red Blend"
-LhbjhkEC8o9TUISCjdx
wine_name: "Ghost Horse"
wine_varietal: "Cabernet Sauvignon"
-LhbjhkEC8o9TUISCjdy
wine_name: "Screaming Eagle"
wine_varietal: "Cabernet Sauvignon, Merlot, Cabernet Franc"
然后单击按钮1时输出
-LhbjhkEC8o9TUISCjdw Scarecrow Red Blend
-LhbjhkEC8o9TUISCjdx Ghost Horse Cabernet Sauvignon
-LhbjhkEC8o9TUISCjdy Screaming Eagle Cabernet Sauvignon, Merlot, Cabernet Franc
请注意,self.ref是对my firebase根节点的引用。您需要引用您的firebase。这是一个非常广泛的问题,因为它涵盖了使用firebase的许多不同方面;书写、阅读、处理DataSnapshots等。此外,我不知道您的数据代表什么,因此我为自己挑选了一些内容,介绍与Firebase合作的一些方面
没有错误检查,但它按原样工作。我一路上都发表了评论
Firebase没有任何对象;只有父节点和子节点。一切都可以像字典一样被视为键:值对。不能写入对象或读取对象。只有NSString、NSNumber、NSDictionary和可怕的NSArray(或其快速对应项)
让我们从一个类开始——有100种方法可以做到这一点,但我喜欢类负责它们的属性,并接受它们并展示它们
class WineClass {
var wine_key = ""
var name = ""
var varietal = ""
//this is used when creating a new wine object before storing in firebase
init(withName: String, andVarietal: String) {
self.name = withName
self.varietal = andVarietal
}
//this is used when we are loading data from firebase to create the wineclass object
init(withSnapshot: DataSnapshot) {
let wineName = withSnapshot.childSnapshot(forPath: "wine_name").value as? String ?? "No Wine Name"
let wineDict = withSnapshot.value as! [String: Any]
let wineVarietal = wineDict["wine_varietal"] as? String ?? "No Wine Varietal"
self.wine_key = withSnapshot.key //when we read a wine, this will be it's reference in case we want to update or delete it
self.name = wineName
self.varietal = wineVarietal
}
//this is use to create a dictionary of key:value pairs to be written to firebase
func getWineDictForFirebase() -> [String: Any] {
let d = [
"wine_name": self.name,
"wine_varietal": self.varietal
]
return d
}
}
然后,我们需要一个类var来存储wine类的。例如,这将是tableView的数据源
var wineArray = [WineClass]() //a class var array to store my wines
然后我会给你们两个按钮,一个用来填充并向Firebase写入一些葡萄酒,另一个用来读入并打印到控制台
func button0() {
self.writeWine(withName: "Scarecrow", andVarietal: "Red Blend")
self.writeWine(withName: "Ghost Horse", andVarietal: "Cabernet Sauvignon")
self.writeWine(withName: "Screaming Eagle", andVarietal: "Cabernet Sauvignon, Merlot, Cabernet Franc")
}
func button1() {
self.readWines()
}
然后函数接受一些字符串作为每种葡萄酒的属性,并将它们写入Firebase
func writeWine(withName: String, andVarietal: String) {
let newWine = WineClass(withName: withName, andVarietal: andVarietal) //create a new wine object
let wineListRef = self.ref.child("wine_list") //get a reference to my firebase wine_list
let thisWineRef = wineListRef.childByAutoId() //a new node for this wine
let d = newWine.getWineDictForFirebase() //get the wine properties as a dictionary
thisWineRef.setValue(d) //save it in firebase
}
最后是一个函数,它读取这些葡萄酒,并在控制台中打印它们的属性
func readWines() {
let wineRef = self.ref.child("wine_list")
wineRef.observeSingleEvent(of: .value, with: { snapshot in //we are reading in the entire wine node which will contain many child nodes
let allWines = snapshot.children.allObjects as! [DataSnapshot] //cast each child node as a DataSnapshot & store in array
for wineSnap in allWines { //iterate over each child node in the array
let wine = WineClass(withSnapshot: wineSnap) //create a new wine, ensuring we also keep track of it's key
self.wineArray.append(wine) //add to the array
}
for wine in self.wineArray {
print(wine.wine_key, wine.name, wine.varietal)
}
})
}
最后,当点击按钮0时,我们的Firebase如下所示
wine_list
-LhbjhkEC8o9TUISCjdw
wine_name: "Scarecrow"
wine_varietal: "Red Blend"
-LhbjhkEC8o9TUISCjdx
wine_name: "Ghost Horse"
wine_varietal: "Cabernet Sauvignon"
-LhbjhkEC8o9TUISCjdy
wine_name: "Screaming Eagle"
wine_varietal: "Cabernet Sauvignon, Merlot, Cabernet Franc"
然后单击按钮1时输出
-LhbjhkEC8o9TUISCjdw Scarecrow Red Blend
-LhbjhkEC8o9TUISCjdx Ghost Horse Cabernet Sauvignon
-LhbjhkEC8o9TUISCjdy Screaming Eagle Cabernet Sauvignon, Merlot, Cabernet Franc
请注意,self.ref是对my firebase根节点的引用。您需要引用您的firebase。一般来说,如果您连接并观察firebase中的某个节点,它将在应用程序启动时自动加载数据(假设调用了该函数),并且该节点内的任何数据更改都将被调用。您是否有机会阅读Firebase入门指南。你对指南的哪一部分或你的代码有困难?嗨,杰,谢谢你的回答。我希望向类中添加成员,并将这些对象保存为firebase实体。我想我正在寻找一个示例,说明如何通过按下按钮添加一个类的对象,将该对象保存到firebase中,然后在再次登录应用程序时读取该对象及其所有属性。这有意义吗?我不知道如何最好地以编程方式添加新对象,而不必手动定义对象本身或通过firebase保存属性;我希望大的答案有帮助!通常,如果您在Firebase中连接并观察到一个节点,它将在应用程序启动时自动加载数据(假设调用了该函数),并且在该节点中的任何时间数据发生更改时,它也将被调用。您是否有机会阅读Firebase入门指南。你对指南的哪一部分或你的代码有困难?嗨,杰,谢谢你的回答。我希望向类中添加成员,并将这些对象保存为firebase实体。我想我正在寻找一个示例,说明如何通过按下按钮添加一个类的对象,将该对象保存到firebase中,然后在再次登录应用程序时读取该对象及其所有属性。这有意义吗?我不知道如何最好地以编程方式添加新对象,而不必手动定义对象本身或通过firebase保存属性;我希望大的答案有帮助!我想我最需要帮助的部分是,我希望我的save函数(即button0)能够使用用户在UITextField
中选择的名称保存newWine
,从而使用用户的