swift - 我正在获取无效的文档引用。文档引用在xcode中必须具有偶数个段错误

标签 swift xcode firebase error-handling

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内部断言失败:无效的文档引用。文档引用必须具有偶数个段”我在做什么错?
this is data structure

最佳答案

您可能是错误的方法。我为您编写了示例代码,可能会对您有所帮助。如果您需要更多帮助,请给我写评论。
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)
    }

}
Post.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
    }
}
这是数据库应具有的结构:
enter image description here

关于swift - 我正在获取无效的文档引用。文档引用在xcode中必须具有偶数个段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62884146/

相关文章:

ios - 通知中心火 UIPasteboard 从 Safari 更改?

ios - 如何在 watchkit 的连续范围内制作动画?

ios - 我如何使用 IBAction 按钮使用 Swift 转到下一个 Storyboard?

swift - 从 Swift 4 转换为 Swift 5 时,Xcode 10.2(和 10.2.1)使整台机器崩溃

firebase - 如果我第二天打开应用程序,Firestore 是否会再次收取文档读取费用?

android - 为什么 Firebase 和 Google Analytics 指标不匹配?

ios - Base64 图像编码 Swift 4 iOS

objective-c - 捏合缩放 - 无法在缩放时保持图像位置固定

android - Firebase 应用内消息传递不断删除显示事件监听器

swift - 在 SwiftUI 文本中显示已发布的整数