- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何使用 Firestore 数据库在 swift 中为 tableview 中的项目列表设置 postkey?
从昨天开始,我尝试了很多东西,但都没有用。我需要使用 postkey 在帖子上发表评论。
下面是我正在关注的代码
发布模型 Swift 文件
import Foundation
import Firebase
import FirebaseFirestore
protocol DocumentSerializable {
init?(dictionary:[String:Any])
}
struct Post {
// var name:String
// var content:String
//var timeStamp:Date
var username: String!
var postTitle: String!
// var postKey: String!
var postcategory: String!
var postContent: String!
//private var _postRef: DocumentReference!
var dictionary:[String:Any] {
return [
"username": username,
//"profile_pic":profile_pic,
"postTitle":postTitle,
"postcategory":postcategory,
"postContent":postContent
// "postKey":postKey
]
}
}
extension Post : DocumentSerializable {
init?(dictionary: [String : Any]) {
guard let username = dictionary["username"] as? String,
// let profile_pic = dictionary["profile_pic"] as? String,
let postTitle = dictionary["postTitle"] as? String,
let postcategory = dictionary["postcategory"] as? String,
let postContent = dictionary["postContent"] as? String else { return nil }
// let postKey = dictionary["postKey"] as? String
self.init(username: username, postTitle: postTitle, postcategory: postcategory, postContent: postContent )
}
}
ViewController 类用于在 tableview 中获取帖子
import Foundation
import UIKit
import Firebase
class HomeViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView:UITableView!
var posts = [Post]()
var db: Firestore!
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()
}
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() { (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()
}
}
}
}
}
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
}
}
最佳答案
它不会是一个简单的帖子 key ,您需要在该列表中创建名为“comments”的帖子的子文档,您将需要添加 commentContent、byUser、toUser、onPost 等。仅添加 postKey 不会解决您的问题
这里是简单的例子
let postId = "45f4854fjrh4" // documentId of post
let commentData = [commentContent:"this is comment", "commenterId":"242343f3ffd3", "postId": postId, "commentName":"\(youProfileName)", "onPost":"45454252342","postOwnerId":"34343434543"
注意:您可以根据需要设置低于或高于数据
Firestore.firestore().collection("posts/\(postId)/Comments").addDocument(commentData)
为了更好地理解,您可以查看下面的房间示例
How to add subcollection to a document in Firebase Cloud Firestore
关于ios - 如何使用 Firestore 数据库快速设置 TableView 中项目列表的后缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57703797/
这似乎是一个愚蠢的问题,但为什么在许多语言中存在 ++ 和 -- 运算符的前缀和后缀版本,但是没有类似 += 或 -= 等其他运算符的前缀/后缀版本?例如,如果我能写出这段代码: myArray[x+
我需要链接到第三方预建共享库。在 Windows 上,这意味着与 Third_party.lib 链接,在 Linux/Android 上,这意味着与 libThird_party.so 链接。所以为
我正在学习 C++ 中的运算符重载。原始后缀++ 的特性是它的优先级低于赋值运算符。例如,int i=0, j=0; i=j++; cout using namespace std; class V
如何在文本区域中添加每行前缀.. 示例: 这是文本区域的内容: hello124 我想为每一行添加一个 [b] 前缀和后缀,这样当我点击一个按钮时,结果将是: [b]hello[/b] [b]124[
背景:在传统的逆波兰表示法中,所有运算符都必须具有固定长度,这使得 RPN 可以很容易地被代码评估和操作,因为每个标记、表达式和子表达式都是“自包含”的,以至于人们可以盲目地替换 y在 x y *为
我有以下旨在修改日期格式的Javascript,但是我想添加原始日期或后缀,例如“st”,“nd”,“rd”,“th”到每个结束日期编号。例如,假设我们当前的日期设置为 4 月 28 日,但我想将日期
我想制定一个 header 检查规则来添加回复并将“发件人”更改为“不回复”。我将它用于某种扩散列表地址 我试过这个正则表达式代码,但它不起作用: if !/^From:(.+@myserver\.f
我想改变数据框的列内容,以便单元格内容以列名作为前缀: > x x VarX VarY 1 A C 2 B D 3 A C 4 B D > x$V
当我执行 ipconfig/all 时,我看到 DNS 后缀搜索列表。我需要从 java 中检索该值。有谁知道如何获得它或它从哪里来? 最佳答案 DNS 后缀列表读取自 HKLM\SYSTEM\Cur
当您编写一个包含大量类的应用程序时,您是否为类名使用了前缀/后缀?还是我应该只依赖我已经定义的 namespace ? 在我的例子中,我有这样的类: Blur Contrast Sharpen Inv
大多数浏览器会像这样显示有序列表: 1. foo 2. bar 3. baz 有没有办法更改编号以改为使用前缀: #1 foo #2 bar #3 baz 最佳答案 这是我能想到的最好的,你只在 Fi
我需要批量重命名多个图像,并希望使用父目录作为基本名称。为防止一个覆盖另一个,必须添加后缀。重命名过程的顺序必须遵循文件的时间戳。因为“第一个”文件是我正在使用它的网站的特色图片。 树: └── ma
我试图使用 sed 替换文件中的一些字符串,但遇到了一个问题。 我有以下字符串: TEMPLATE_MODULE TEMPLATE_SOME_ERR TEMPLATE_MORE_ERR 我想用一些字符
我对后缀/前缀运算符的优先级 和关联性 感到困惑。 一方面,当我阅读 K&R 书时,它指出: (*ip)++ The parentheses are necessary in this last ex
我有一个具有以下结构的图 V = {A1, A2, A3, A4, A5, .., An} E = {E1, E2, E3, E4, .., Ek} 现在我们定义A1的后缀: S(A1) = {All
这是解释性代码。语言是Java,代码使用Android。 fg.setTextSize(height*0.50f); //<-'f' is in the brackets 或 @Override pr
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我正在编写自己的数组类作为练习。因为,我读到非成员函数实际上在某些方面比成员函数更好。( Scott Meyers ) 我正在尝试编写尽可能多的运算符重载作为非成员函数。运算符重载 + , - 作为非
谁能解释一下关于 C 编程语言的中缀、后缀和前缀表示法是什么? 最佳答案 这是对 three terms, and how they apply 的一个很好的讨论. C 语言几乎到处都使用中缀表示法。
我有这种情况:我需要在输入文本字段 (html) 中添加引号而不更改输入的值。我正在使用 angular,所以我使用 ngModel,它看起来像这样 我希望输入字段显示“{{data}} 中的任何内
我是一名优秀的程序员,十分优秀!