gpt4 book ai didi

swift - 如何在另一个类中获取在一个类中声明的变量?

转载 作者:行者123 更新时间:2023-11-28 07:19:00 24 4
gpt4 key购买 nike

我刚开始使用 Swift/firebase,抱歉我的笨拙。我正在使用 childbyautoid 将数据写入 firebase 数据库。我想访问另一个类中的最后一个键/ID。

    let postName = Database.database().reference().child("Event").childByAutoId()

let postNameObject = [
"EventName": NameTextField.text,
"timestamp": [".sv":"timestamp"],
"userID": Auth.auth().currentUser!.uid
] as [String:Any]

postName.setValue(postNameObject, withCompletionBlock: { error, ref in
if error == nil {
self.dismiss(animated: true, completion: nil)
} else {
}
})
let childautoID = postName.key

我希望能够在另一个类中调用 childautoID 我们找到另一种方法来更新此节点。

最佳答案

您也许可以创建一个单例类来管理 Firebase 方法。每当使用 Firebase 构建应用程序时,我都会做类似的事情。这使您可以在多个类中全局引用值并使用与 Firebase 相关的方法。在我采用这种方法之前,我发现自己重写了相同的代码来将对象上传到 firebase。单例可让您创建可重复使用的代码,包括在此全局类中设置的“最后一个 key ”的存储。

class FirebaseManager {

//You've probably seen something similar to this "shared" in other Apple frameworks
//Maybe URLSession.shared or FileManager.default or UserDefaults.standard or SKPaymentQueue.default() or UIApplication.shared
static var shared = FirebaseManager()

func createEvent(name: String, timeStamp: [String:String], uid: String) {
let postNameObject = [
"EventName": name,
"timestamp": timeStamp,
"userID": uid
] as [String:Any]

postName.setValue(postNameObject, withCompletionBlock: { error, ref in
if error == nil {
self.dismiss(animated: true, completion: nil)
} else {
//Do something
}
})
let childautoID = postName.key

//Whatever else you need to do in the function below here...

}

}

用法:

class SomeViewController: UIViewController {

@IBOutlet var nameTextField: UITextField!

//Some code...

@IBAction func createEventButtonPressed(_ sender: Any) {
FirebaseManager.shared.createEvent(name: nameTextField.text, timeStamp: [".sv":"timestamp"], uid: Auth.auth().currentUser!.uid)
}

//Some more code...
}

同样,您可以在我们的 FirebaseManager 类中添加一个类型为 String 的名为 lastKey 的值。请注意我们添加到 FirebaseManager 类顶部的变量:

class FirebaseManager {

var lastKey: String!

static var shared = FirebaseManager()

func createEvent(name: String, timeStamp: [String:String], uid: String) {
let postNameObject = [
"EventName": name,
"timestamp": timeStamp,
"userID": uid
] as [String:Any]

postName.setValue(postNameObject, withCompletionBlock: { error, ref in
if error == nil {
self.dismiss(animated: true, completion: nil)
} else {
//Do something
}
})
let childautoID = postName.key

//Whatever else you need to do in the function below here...

}

}

同样,我们可以在一个 View Controller 中设置这个值:

class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
FirebaseManager.shared.lastKey = "aBcDeFg9876543210"
}
}

然后在前一个 View Controller 之后加载的另一个 View Controller 中获取此值:

class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(FirebaseManager.shared.lastKey)
}
}

打印:aBcDeFg9876543210

这就是美static关键词。您可以在此处了解有关创建单例类的更多信息:https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift

关于swift - 如何在另一个类中获取在一个类中声明的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701000/

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