簡體   English   中英

我得到無效的文檔參考。 文檔引用必須在 xcode 中有偶數段錯誤

[英]Im getting Invalid document reference. Document references must have an even number of segments error in xcode

class RoomFeed: UIViewController, UITableViewDelegate, UITableViewDataSource{
let posts = Post(id: self.documentId , author: owner!, text: text!, amount: self.amount)
//This is called in the view controller which is "host of table View" 
//Document Id at this point is not nill confirmed.


}





class PostTableVIewCell: UITableViewCell {
//This is table view cell

var king: String!
var postID: String!

 override func awakeFromNib() {
        super.awakeFromNib()

    Firestore.firestore().collection("GeneralData").document("\(self.postID)").getDocument { document, err in
        if let document = document, document.exists {
            let data = document.data()
            self.king = data!["1"] as? String
            let placeamount = data!["amount"] as? String
            self.amountLabel.text = placeamount
    
        }
        print("KING: \(self.king!)")
        self.kingLabel.text = self.king
    }}

    func set(post:Post) {
         
            usernameLabel.text = post.author
            PostTextLabel.text = post.text
            self.amount = post.amount
          postID = post.id
        }
}

這是我的代碼,但我收到一條錯誤消息,指出“'FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have a even number of segments”我做錯了什么?

這是數據結構

你可能是錯誤的方法。 我給你寫了一個可能對你有幫助的示例代碼。 如果您需要更多幫助,請給我寫評論。

ViewController.swift

import UIKit
import Firebase

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var postDataList = [Post]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    
        Firestore.firestore().collection("GeneralData").getDocuments { document, err in
            if let document = document {
                let dataDescription = document.documents
                    
                for i in dataDescription {
                    let post = Post.init(data: i.data())
                    self.postDataList.append(post)
                }
                
                self.tableView.reloadData()
            }
            else {
                print("Document does not exist")
            }
        }
    }
    
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.postDataList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
        
        let post = self.postDataList[indexPath.row]
        cell.setPost(post: post)
        
        return cell
    }
    
}

CustomTableViewCell.swift

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var postTextLabel: UILabel!
    @IBOutlet weak var amountLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func setPost(post: Post) {
        self.usernameLabel.text = post.name
        self.postTextLabel.text = post.text
        self.amountLabel.text = String(post.amount)
    }

}

郵政swift

struct Post {
    let name: String
    let surname: String
    let owner: String
    let amount: Int
    let text: String
    let uid: String
    
    init(data: [String: Any]) {
        self.name = data["name"] as! String
        self.surname = data["surname"] as! String
        self.owner = data["owner"] as! String
        self.amount = data["amount"] as! Int
        self.text = data["text"] as! String
        self.uid = data["uid"] as! String
    }
}

這是您的數據庫應該具有的結構: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM