此错误是我遇到的众多问题之一:
类型'DataService'没有成员'dataService'
我一直在寻找,但找不到任何确定的东西。感谢您的协助。我不确定是什么问题。我以为起初该服务已更改为数据库,但是除非我完全错误,否则这也不对。
import UIKit
import Firebase
class UserFeedTableViewController: UITableViewController {
let ref = Database.database().reference(withPath: "userpost-items")
var userpost = [UserPost]()
override func viewDidLoad() {
super.viewDidLoad()
// observeEventType is called whenever anything changes in the Firebase - new Jokes or Votes.
// It's also called here in viewDidLoad().
// It's always listening.
DataService.dataService.USERPOST_REF.observe(.value, with: { snapshot in
// The snapshot is a current look at our jokes data.
self.userposts = []
if let snapshots = snapshot?.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
// Make our jokes array for the tableView.
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let userpost = UserPost(key: key!, dictionary: postDictionary)
// Items are returned chronologically, but it's more fun with the newest jokes first.
self.userpost.insert(userpost, at: 0)
}
}
}
// Be sure that the tableView updates when there is new data.
self.tableView.reloadData()
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userpost.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let userpost = self.userpost[indexPath.row]
// We are using a custom cell.
if let cell = tableView.dequeueReusableCell(withIdentifier: "UserPostCellTableViewCell") as? UserPostTableViewCell {
// Send the single user post to configureCell() in UserPostCellTableViewCell.
cell.configureCell(userpost: userpost)
return cell
} else {
return UserPostTableViewCell()
}
}
}
答案 0 :(得分:0)
我发表了评论,但实际代码将更清楚地阐明我建议的模式。还有100种其他方式可以做到这一点。 (注意:Swift 4,Firebase 4)
class UserFeedTableViewController: UITableViewController {
var ref: DatabaseReference!
var userpost = [UserPost]()
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
}
func loadPosts() {
let postRef = self.ref.child("posts")
postRef.observe(.value, with: { snapshot in
self.userposts = []
...
}