gpt4 book ai didi

ios - 在 Xcode 9.2 中检索 Firebase 数据库引用时出错

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

我想尝试遵循一些在线教程来使应用程序连接到 Firebase,但它们已经过时了,因为它们仍在使用 FIRApp.configure()等等。因此我必须相应地更改代码,并且无法拥有此类应用程序的完整可用版本。

到目前为止,我已经:

  1. 通读documentation from Firebase

  2. 如何在 AppDelegate 中定义“ref”变量 ( How to fix Firebase SIGABRT crash iOS ),

  3. 如何在 AppDelegate 中将该变量设置为全局变量并在其他 ViewController ( How do I get a reference to the app delegate in Swift? ) 中可读,

  4. 检查我是否包含了正确的 pod 文件,并在 viewDidLoad 部分 ( Where do I add the database reference from Firebase on iOS? ) 中设置变量的值,

  5. 注意到 pod 文件可能需要更新,执行了 pod disintegrate 和 pod update ( Get a database reference in the new Firebase 4.0 release )。

对于 podfile,我已包含:

pod 'Firebase/Core'
pod 'Firebase/Database'

对于AppDelegate,(未显示的部分为默认)

import UIKit
import CoreData
import Firebase
import FirebaseDatabase //tried not having this line, still the same

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var ref: DatabaseReference!

func application(_application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
self.ref = Database.database().reference()
return true
}

对于 ViewController,(未显示的部分是默认的或肯定不相关)

import UIKit
import Firebase
import FirebaseDatabase //again, tried with/without this line

class ViewController: UIViewController {

@IBOutlet weak var emailfield: UITextField!
@IBOutlet weak var usrnamefield: UITextField!
@IBOutlet weak var pwdfield: UITextField!
@IBOutlet weak var loginbtn: UIButton!
@IBOutlet weak var createnewaccbtn: UIButton!
@IBOutlet weak var forgetpwdbtn: UIButton!
//......
@IBAction func createnewAcc(_ sender: UIButton) {
let username = usrnamefield.text
let email = emailfield.text
let password = pwdfield.text

if username != "" && email != "" && password != "" {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.ref.child("users").setValue(["username": username]) //this line is highlighted with the error below
//Cannot even update one single field to the database
}
}
}

但是当我运行模拟器时,出现错误

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

引用:

Xcode 9.2,CocoaPods 1.4.0,部署目标:9.2、10.3

更新(2018 年 2 月 27 日,GMT 18:00):

尝试定义 databaseReference ViewController 中的变量直接,并使用 databaseReference = Database.database().reference() 赋值里面viewDidLoad .

结果收到signal SIGABRT在 AppDelegate 中( class AppDelegate 行)。禁用上述赋值代码将解决 signal SIGABRT问题,但仍然无法连接到 Firebase 数据库...

最佳答案

那么,您在代码中显示的内容是在 firebase 中编写而不是检索。现在的要点是,如果您想编写,则首先使用一些常量值(如果有效意味着 firebase 连接正确,否则文本字段或创建数据库引用问题)。有什么原因不在您想在 firebase 中读取或写入的同一个 ViewController 中提供引用吗?

关于ios - 在 Xcode 9.2 中检索 Firebase 数据库引用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995789/

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