在swift 2中从firebase调用child时出现错误的指令错误

时间:2017-05-12 18:12:48

标签: swift xcode firebase swift2

我们正在尝试访问数据库中的子项,但是我们正在使用来自segue引入的先前视图的字符串。它不断给出错误指令的错误。仅在模拟器中运行后才会出现。我们正在寻找解决错误的方法。谢谢!

从上一个视图中使用字符串函数:

    }
    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! SorDViewController
        if segue.identifier == "secondScreen" {
            controller.textToDisplay = nameField.text
        }
    }

用于访问和上传数据到firebase的代码(错误仅出现在第一个函数中):

 import Foundation
 import UIKit
 import Firebase
 import FirebaseDatabase

 class SorDViewController: UIViewController {

     var textToDisplay: String?

     @IBAction func postData(sender: AnyObject) {
         let ref = FIRDatabase.database().referenceFromURL("database URL")
         ref.child(textToDisplay!).child("Type").setValue("Singles")
     }

     @IBAction func postData2(sender: AnyObject) {
         let ref = FIRDatabase.database().referenceFromURL("database URL")
         ref.child("Type").setValue("Doubles")
     }

 }

1 个答案:

答案 0 :(得分:0)

作为快速测试,请尝试更改prepareForSegue方法:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let controller = segue.destinationViewController as! SorDViewController
    if segue.identifier == "secondScreen" {
        controller.textToDisplay = nameField.text
    }
}

到此:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let controller = segue.destinationViewController as! SorDViewController
    print("=> nameField: \(nameField.text)")
    controller.textToDisplay = nameField.text
}

请仔细检查您的nameField.text实际上是否包含非零值:)

跟踪textToDisplay的更改也是一个好主意:

var textToDisplay: String? {
    didSet {
        print("oldValue: \(oldValue), newValue: \(textToDisplay)")
    }
}