布尔条件在 Swift 中不起作用
Boolean condition is not working in Swift
我正在使用消息套件。在 chatDashbaord 扩展中,我有布尔条件,比如如果 isnewConversation 包含 true 则调用函数 insertConversation2 如果 isnewConversation 包含 false 则调用函数 sendMessag.But 我已经使用断点检查了代码不会进入 isNewconversation 块我不知道为什么 happening.When 我没有在代码工作文件中使用布尔语句并将数据附加到数组中,请检查代码谢谢。
消息视图控制器:
class ChatDashboard: MessagesViewController {
var userActive: String? = nil
var receiverName:String? = nil
var isnewConversation = false
var recevierName:String? = nil
var otheruserEmail: String? = nil
var conversationId:String? = nil
var messages : [Message] = []{
didSet{
DispatchQueue.main.async {
self.messagesCollectionView.reloadDataAndKeepOffset()
}
}
分机 ChatDashboard:
extension ChatDashboard: InputBarAccessoryViewDelegate{
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard !text.replacingOccurrences(of: "", with: "").isEmpty,let selfSender =
self.selfSender,let messageId = createMessageId() else{
return
}
let message = Message(sender: selfSender, messageId: messageId, sentDate: Date(), kind: .text(text))
guard let otheruserEmail = otheruserEmail,let recevierName = recevierName else {return}
if isnewConversation{
NetworkingService.shared.insertConversation2(with:otheruserEmail,name:recevierName, message: message) { (success) in
if success{
print("message Send")
self.isnewConversation = false
}
else{
print("not send")
}
}
}
else{
guard let converId = conversationId else {return}
NetworkingService.shared.sendMessage(converId,recevierName,message) { (success) in
if success{
print("message send in")
}
else{
print("message not send")
}
}
}
}
}
}
条件为假时的 SendMessage 函数代码:
func sendMessage(_ convesationId:String,_ name:String,_ message:Message,completion:@escaping(Bool) -> Void){
self.sendMessageReciverToSender(convesationId) { (user) in
guard var receiverToSenderNdoe = user else {completion(false); return}
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
let currentUserEmail = getUserEmail(currentEmail: email)
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
default:
break
}
let newMessge: [String:Any] = [
"id":message.messageId,
"type":message.kind.messageKindString,
"content":messageData,
"date": dateToString,
"sender_email":currentUserEmail,
"is_read":false,
"name":name
]
//issues is here
if var conversationData = receiverToSenderNdoe["messages"] as? [[String:Any]]{
conversationData.append(newMessge)
receiverToSenderNdoe["messages"] = conversationData
self.reciverToSenderSave(convesationId, newMessge) { (result) in
if result{
completion(true)
}
else{
completion(false)
}
}
}
}
}
在 sendMessage 函数中,这两个函数被调用
func sendMessageReciverToSender(_ conversationId:String,completion:@escaping([[String:Any]]?) -> Void){
database.child("\(conversationId)/messages").observeSingleEvent(of: .value) { (snapshot) in
if let currentUser = snapshot.value as? [[String:Any]]{
completion(currentUser)
}
else{
print("errro in reciver to sender sendMessageReciverToSender func ")
completion(nil)
}
}
}
func reciverToSenderSave(_ conversationId:String,_ conversation:[[String:Any]],completion:@escaping(Bool)-> Void){
database.child("\(conversationId)/messages").setValue(conversation){(error,ref) in
if error == nil{
completion(true)
}
else{
print("eroor in receiptin \(error?.localizedDescription)")
completion(false)
}
}
}
您的 sendMessage
return without
在这里呼叫 completion
guard var receiverToSenderNdoe = user else {return}
应该是
guard var receiverToSenderNdoe = user else { completion(false) ; return }
这里
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
return
}
应该是
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
您必须确保所有路径都得到处理,以便在 success/failure 中调用您的回调,顺便说一句,像这样
if result{
completion(true)
}
else{
completion(false)
}
可能很快
completion(result)
我正在使用消息套件。在 chatDashbaord 扩展中,我有布尔条件,比如如果 isnewConversation 包含 true 则调用函数 insertConversation2 如果 isnewConversation 包含 false 则调用函数 sendMessag.But 我已经使用断点检查了代码不会进入 isNewconversation 块我不知道为什么 happening.When 我没有在代码工作文件中使用布尔语句并将数据附加到数组中,请检查代码谢谢。
消息视图控制器:
class ChatDashboard: MessagesViewController {
var userActive: String? = nil
var receiverName:String? = nil
var isnewConversation = false
var recevierName:String? = nil
var otheruserEmail: String? = nil
var conversationId:String? = nil
var messages : [Message] = []{
didSet{
DispatchQueue.main.async {
self.messagesCollectionView.reloadDataAndKeepOffset()
}
}
分机 ChatDashboard:
extension ChatDashboard: InputBarAccessoryViewDelegate{
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard !text.replacingOccurrences(of: "", with: "").isEmpty,let selfSender =
self.selfSender,let messageId = createMessageId() else{
return
}
let message = Message(sender: selfSender, messageId: messageId, sentDate: Date(), kind: .text(text))
guard let otheruserEmail = otheruserEmail,let recevierName = recevierName else {return}
if isnewConversation{
NetworkingService.shared.insertConversation2(with:otheruserEmail,name:recevierName, message: message) { (success) in
if success{
print("message Send")
self.isnewConversation = false
}
else{
print("not send")
}
}
}
else{
guard let converId = conversationId else {return}
NetworkingService.shared.sendMessage(converId,recevierName,message) { (success) in
if success{
print("message send in")
}
else{
print("message not send")
}
}
}
}
}
}
条件为假时的 SendMessage 函数代码:
func sendMessage(_ convesationId:String,_ name:String,_ message:Message,completion:@escaping(Bool) -> Void){
self.sendMessageReciverToSender(convesationId) { (user) in
guard var receiverToSenderNdoe = user else {completion(false); return}
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
let currentUserEmail = getUserEmail(currentEmail: email)
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
default:
break
}
let newMessge: [String:Any] = [
"id":message.messageId,
"type":message.kind.messageKindString,
"content":messageData,
"date": dateToString,
"sender_email":currentUserEmail,
"is_read":false,
"name":name
]
//issues is here
if var conversationData = receiverToSenderNdoe["messages"] as? [[String:Any]]{
conversationData.append(newMessge)
receiverToSenderNdoe["messages"] = conversationData
self.reciverToSenderSave(convesationId, newMessge) { (result) in
if result{
completion(true)
}
else{
completion(false)
}
}
}
}
}
在 sendMessage 函数中,这两个函数被调用
func sendMessageReciverToSender(_ conversationId:String,completion:@escaping([[String:Any]]?) -> Void){
database.child("\(conversationId)/messages").observeSingleEvent(of: .value) { (snapshot) in
if let currentUser = snapshot.value as? [[String:Any]]{
completion(currentUser)
}
else{
print("errro in reciver to sender sendMessageReciverToSender func ")
completion(nil)
}
}
}
func reciverToSenderSave(_ conversationId:String,_ conversation:[[String:Any]],completion:@escaping(Bool)-> Void){
database.child("\(conversationId)/messages").setValue(conversation){(error,ref) in
if error == nil{
completion(true)
}
else{
print("eroor in receiptin \(error?.localizedDescription)")
completion(false)
}
}
}
您的 sendMessage
return without
在这里呼叫 completion
guard var receiverToSenderNdoe = user else {return}
应该是
guard var receiverToSenderNdoe = user else { completion(false) ; return }
这里
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
return
}
应该是
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
print("Email not found in user default")
completion(false)
return
}
您必须确保所有路径都得到处理,以便在 success/failure 中调用您的回调,顺便说一句,像这样
if result{
completion(true)
}
else{
completion(false)
}
可能很快
completion(result)