感谢在我的另一个 SA 问题上帮助我的人,我能够创建一个返回布尔值的函数,以查看用户是否已经对聊天消息进行了投票。我想打印该人是否使用 MessageKitmessageBottomLabelAttributedText
功能对聊天消息进行了投票。但是,我无法使用返回的布尔值来打印正确的文本。
这是我当前在 MessagesDataSource 中的 messageBottomLabelAttributedText 函数:
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
var bool = didAlreadyVote(message: message as! MessageType){_ in Bool.self}
if bool as? Bool == true {
let dateString = self.formatter.string(from: message.sentDate)
let likeString = "Voted"
return NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
} else {
let dateString = self.formatter.string(from: message.sentDate)
return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
}
}
}
作为参考,这里是这个社区早些时候帮助我的 didAlreadyVote 函数:
func didAlreadyVote(message: MessageType, completion: @escaping (Bool) -> Void) {
// check user votes collection to see if current message matches
guard let currentUser = Auth.auth().currentUser else {return}
let userID = currentUser.uid
let docRef = Firestore.firestore().collection("users").document(userID).collection("upvotes").whereField("messageId", isEqualTo: message.messageId)
docRef.getDocuments { querySnapshot, error in
if let error = error {
print("Error getting documents: \(error)")
completion(false)
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
completion(true) /// Note that this will get called multiple times if you have more the one document!
}
}
}
}
当我运行应用程序时,布尔变量不返回任何内容。如何从函数中检索布尔值,然后在 messageBottomLabelAttributedText 中使用它?
谢谢!