【问题标题】:getting the error of NSUnknownKeyException', reason: setValue:forUndefinedKey: this class is not key value coding-compliant for the key description得到 NSUnknownKeyException' 的错误,原因:setValue:forUndefinedKey: 这个类不符合键描述的键值编码
【发布时间】:2020-01-05 04:29:17
【问题描述】:

我面临将数据从 HomeViewController 传递到 PostDetailViewController 的问题, 我检查了连接到视图控制器的类是否正确,连接到XIB文件的类是PostTableViewCell,

并且仍然得到这个错误

'NSUnknownKeyException',原因: '[ setValue:forUndefinedKey:]:这个类不是键值 密钥描述的编码兼容

点击表格单元格时

HOMEVIEWCONTROLLER

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView:UITableView!

    var posts = [Post]()
    var db: Firestore!

    var postKey:String = ""
    private var documents: [DocumentSnapshot] = []
    //public var posts: [Post] = []
    private var listener : ListenerRegistration!




    override func viewDidLoad() {
        super.viewDidLoad()

        db = Firestore.firestore()

        self.navigationController?.navigationBar.isTranslucent = false
        tableView = UITableView(frame: view.bounds, style: .plain)

        let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
        tableView.register(cellNib, forCellReuseIdentifier: "postCell")
        tableView.backgroundColor = UIColor(white: 0.90,alpha:1.0)
        view.addSubview(tableView)

        var layoutGuide:UILayoutGuide!

        if #available(iOS 11.0, *) {
            layoutGuide = view.safeAreaLayoutGuide
        } else {
            // Fallback on earlier versions
            layoutGuide = view.layoutMarginsGuide
        }

        tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
        retrieveAllPosts()
        //checkForUpdates()

    }


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


    @IBAction func handleLogout(_ sender:Any) {
        try! Auth.auth().signOut()
        self.dismiss(animated: false, completion: nil)
    }


    func retrieveAllPosts(){
        let postsRef = Firestore.firestore().collection("posts").limit(to: 50)

        postsRef.getDocuments { (snapshot, error) in

            if let error = error {

                print(error.localizedDescription)

            } else {

                if let snapshot = snapshot {

                    for document in snapshot.documents {


                        let data = document.data()
                       // self.postKey = document.documentID
                        let username = data["username"] as? String ?? ""
                        let postTitle = data["postTitle"] as? String ?? ""
                        let postcategory = data["postcategory"] as? String ?? ""
                        let postContent = data["postContent"] as? String ?? ""

                        let newSourse = Post( _documentId: document.documentID, _username: username, _postTitle: postTitle, _postcategory: postcategory, _postContent: postContent)
                        self.posts.append(newSourse)
                        print(self.postKey)
                    }
                    self.tableView.reloadData()
                }
            }
        }
    }

       /* postsRef.getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")


                    self.posts = querySnapshot!.documents.flatMap({Post(dictionary: $0.data())})
                    DispatchQueue.main.async {
                        self.tableView.reloadData()

        }
}
            }
        }*/
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    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! PostTableViewCell
        cell.set(post: posts[indexPath.row])
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let post = self.posts[indexPath.row]
        //print("row selected: \(indexPath.row)")
        //Swift.print(post._documentId!)
       let postKey = post._documentId
        let postName = post._username
        print(postKey! + postName!)
        performSegue(withIdentifier: "toDetailView", sender: indexPath)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       //segue.forward(posts, to: segue.destination)
        guard let details = segue.destination as? PostDetailViewController,
        let index = tableView.indexPathForSelectedRow?.row

       else {
        return
        }
        details.detailView = posts[index]



    }

}

POSTTABLEVIEWCELL

class PostTableViewCell: UITableViewCell {

    @IBOutlet weak var usernameLabel: UILabel!
   @IBOutlet weak var profileImageView: UIImageView!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var postTextLabel: UILabel!



    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

       // profileImageView.layer.cornerRadius = profileImageView.bounds.height / 2
       // profileImageView.clipsToBounds = true
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func set(post:Post) {
        usernameLabel.text = post._username
        postTextLabel.text = post._postTitle
        subtitleLabel.text = post._postcategory
    }

}

发布细节视图控制器
类 PostDetailViewController: UIViewController {

    @IBOutlet var usernamelabel: UILabel!


    @IBOutlet var posttitlelabel: UILabel!

    @IBOutlet var postIdlabel: UILabel!

   // @IBOutlet var description: UILabel!
    @IBOutlet var postcategorylabel: UILabel!

    var detailView: Post?
    var postId:String = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        postIdlabel?.text = detailView?._documentId
        posttitlelabel?.text = detailView?._postTitle

    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

【问题讨论】:

    标签: ios swift firebase uitableview


    【解决方案1】:

    这种情况通常发生在您已经在 XIB 文件中设置了 IBOutlet 并在代码中注释掉它正在连接的出口时。

    在你的情况下,在你的 PostDetailViewController

    // @IBOutlet var description: UILabel!
    

    您已经注释了描述标签,但您的XIB文件中仍然连接了IBOutlet。

    因此,查找您的 XIB 文件并检查活动的 IBOutlet 连接并将其删除以获取描述标签、清理、构建和运行。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2022-01-16
      相关资源
      最近更新 更多