我在行self.fetchUser(uid: post.uid!, completed: {
上运行项目时得到提示,这意味着它解开了nil值。但是我不认为post.uid
是nil值,所以我不确定为什么会收到此错误?
class HomeViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var posts = [Post]()
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
loadPosts()
}
func loadPosts() {
Api.Post.observePosts { (post) in
self.fetchUser(uid: post.uid!, completed: {
self.posts.append(post)
self.tableView.reloadData()
})
}
}
func fetchUser(uid: String, completed: @escaping () -> Void ) {
Api.User.observeUser(withId: uid, completion: {
user in
self.users.append(user)
completed()
})
}
}
extension HomeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! HomeTableViewCell
let post = posts[indexPath.row]
let user = users[indexPath.row]
cell.user = user
cell.post = post
cell.delegate = self
return cell
}
}