2017-01-08 37 views
2

这是我在得到一个错误的行:错误说“类型的值‘FIRDatabaseReference’没有成员‘观察’”

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

正确的代码块应该是:

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

你这是正确的代码和代码之间的上述通知是什么?查看完成块。矿是和你的是与块。编码时,使用自动完成功能。例如,您键入这段代码,Xcode现在会询问您如何处理Firebase代码的完成代码块。你应该只需双击完成块,Xcode就会为你解决所有问题。

+0

谢谢你的回应。我用Block更改为,但仍然收到相同的错误信息 –

+0

使用自动完成建议我能够使其工作。代码结束为:databaseHandle = ref.child(“Posts”)。observeEventType(.ChildAdded,withBlock:{(snapshot)in self.postData.append(“”)...谢谢你的帮助! –

+0

哦!可能我们使用的是不同版本的Firebase SDK,不管怎样,以后只需使用Xcode的自动完成/建议:D很高兴现在解决它了。您可以选择这个作为您的文章的答案吗? – Glenn

相关问题