gpt4 book ai didi

swift - 等待另一个函数在 MessageKit 函数中完成

转载 作者:行者123 更新时间:2023-11-28 13:49:05 24 4
gpt4 key购买 nike

感谢在我的另一个 SA 问题上帮助我的人,我能够创建一个返回 bool 值的函数来查看用户是否已经对聊天消息进行了投票。如果有人使用 MessageKit messageBottomLabelAttributedText 函数对聊天消息进行投票,我想打印出来。但是,我无法使用返回的 bool 值来打印正确的文本。

这是我当前在 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!
}
}
}
}

当我运行应用程序时,bool 变量没有返回任何内容。如何从函数中检索 bool 值,然后在 messageBottomLabelAttributedText 中使用它?

谢谢!

最佳答案

不要等待。

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)]))
}
}
}

关于swift - 等待另一个函数在 MessageKit 函数中完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com