Swift-意外地发现nil解开了Optional值

时间:2018-06-26 16:04:25

标签: swift xcode firebase null

我在行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
    }
}

0 个答案:

没有答案