0

感谢在我的另一个 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 中使用它?

谢谢!

4

1 回答 1

1

不要等待。

didAlreadyVote返回任何东西。它包含一个异步完成处理程序,该处理程序将Bool值作为参数传递。

语法是

    didAlreadyVote(message: message){ boolValue in   
        if boolValue {
            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)])
        }

但是messageBottomLabelAttributedText无论如何你都不能使用它,因为你不能返回任何东西,除非你也实现了一个完成处理程序,例如

func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath, completion: @escaping (NSAttributedString) -> Void) {
    didAlreadyVote(message: message){ boolValue in   
        if boolValue {
            let dateString = self.formatter.string(from: message.sentDate)
            let likeString = "Voted"
            completion(NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]))
        } else {
            let dateString = self.formatter.string(from: message.sentDate)
            completion(NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)]))
        }
    }
}
于 2019-03-04T20:43:27.333 回答