gpt4 book ai didi

swift - Firebase 数据与 Eureka 和 Init

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

我对如何使用 init 使用 Eureka 正确将数据保存到 firebase 感到困惑。我查看的资源似乎并没有澄清我的答案。不确定执行此操作的最佳方法。

我有一个名为 User.swift 的文件,其中包含一个结构:

`struct UsersInfo {

let email: String
let password: String
let first: String
let last: String
let about: String
let age: Int
let city: String
let firebaseReference: DatabaseReference?

init(email: String, password: String, first: String, last: String, about: String, age: Int, city: String) {
self.email = email
self.password = password
self.first = first
self.last = last
self.about = about
self.age = age
self.city = city
self.firebaseReference = nil
}

init(snapshot: DataSnapshot) {
let snapshotValue = snapshot.value as! [String : AnyObject]
self.email = snapshotValue["email"] as! String
self.password = snapshotValue["password"] as! String
self.first = snapshotValue["first"] as! String
self.last = snapshotValue["last"] as! String
self.age = snapshotValue["age"] as! Int
self.city = snapshotValue["city"] as! String
self.about = snapshotValue["about"] as! String
self.firebaseReference = snapshot.ref
}
} // End Struct.

然后我有一个文件/ View Controller ,它使用 FormViewController 导入 Eureka。我设置为值的 Eureka 行位于此文件中,如下所示:

    // Get the value of a single row
let emailrow: EmailRow? = form.rowBy(tag: "emailRowTag")
let emailvalue = emailrow?.value
// Get the value of a single row
let passwordrow: PasswordRow? = form.rowBy(tag: "passwordRowTag")
let passwordvalue = passwordrow?.value
// Get the value of a single row
let firstrow: NameRow? = form.rowBy(tag: "firstRowTag")
let firstvalue = firstrow?.value
// Get the value of a single row
let lastrow: TextRow? = form.rowBy(tag: "lastRowTag")
let lastvalue = lastrow?.value
// Get the value of a single row
let aboutrow: TextRow? = form.rowBy(tag: "aboutRowTag")
let aboutvalue = aboutrow?.value
// Get the value of a single row
let agerow: IntRow? = form.rowBy(tag: "ageRowTag")
let agevalue = agerow?.value
// Get the value of a single row
let cityrow: PushRow<String>? = form.rowBy(tag: "cityRowTag")
let cityvalue = cityrow?.value

在此文件/ View Controller 中,我有一个使用 Eureka 的 ButtonRow 的自定义按钮。我实现了一个函数来执行此操作,并将其命名为 onCellSelection。行的值让我对 Firebase 感到困惑,以及我需要将其放入 User.swift 文件中的 snapshotValue 字典中并在 FormViewController 中执行。谢谢!

最佳答案

我做的第一件事是给 TextRow 类型的行一个标签

所以我有类似的东西

row.tag = "username"

然后我就可以使用以下代码检索该标签

let usernameRow: TextRow? = form.rowBy(tag: "username")

let username: String? = usernameRow?.cell.textField.text

Firebase 只允许将字符串推送到数据库,因此现在您可以调用 setValue 函数并传递变量用户名

firebaseref.setValue("username": username)

关于swift - Firebase 数据与 Eureka 和 Init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441481/

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