错误说“类型'FIRDatabaseReference'的值没有成员'观察'”

时间:2017-01-08 01:54:46

标签: ios swift xcode

以下是我收到错误的行:

databaseHandle = ref.child("Posts").observe(.childAdded, withBlock: { (snapshot) in
        self.postData.append("")
    })

以下是所有代码......

import UIKit
import FirebaseDatabase

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet
    var tableView: UITableView!

        var ref: FIRDatabaseReference!
            var databaseHandle: FIRDatabaseHandle ?

                var postData = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

        tableView.delegate = self
        tableView.dataSource = self



        ref = FIRDatabase.database().reference()

        databaseHandle = ref.child("Posts").observe(.childAdded, withBlock: {
            (snapshot) in
            self.postData.append("")
        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
    }

    @IBAction func JumpTabLive(sender: AnyObject) {

        tabBarController ? .selectedIndex = 1
    }

    @IBAction func JumpTabLocal(sender: AnyObject) {

        tabBarController ? .selectedIndex = 2
    }

    @IBAction func JumpTabOnline(sender: AnyObject) {

        tabBarController ? .selectedIndex = 3
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int {

        return postData.count

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("PostCell")
        cell ? .textLabel ? .text = postData[indexPath.row]

        return cell!


    }


}

1 个答案:

答案 0 :(得分:1)

正确的代码块应该是:

    databaseHandle = ref.child("Posts").observe(.childAdded, with: {
        (snapshot) in
        self.postData.append("")
    })

您在上述正确的代码与上述代码之间注意到了什么?查看完成块。我的 ,你的 withBlock 。编码时,使用自动完成功能。例如,您键入此代码块,Xcode现在会询问您如何处理Firebase代码的完成块。您应该双击完成块,Xcode将为您修复所有内容。

相关问题