前提・実現したいこと
MessageKitを用いてチャット機能を実装しようとしています。現在チャット一覧からチャットルームをクリックするとMessagesCollectionViewにメッセージが表示出来るのですが、その後チャットルームを退出し、チャットルーム一覧に戻って同じチャットルームをクリックすると以下のエラーメッセージが表示されクラッシュします。
発生している問題・エラーメッセージ
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file
というエラーが
func currentSender() -> SenderType { return currentUser }
のreturn currentUserに対して表示され原因が分かりません。
###ソースコード
import UIKit import MessageKit import MessageInputBar import InputBarAccessoryView import FirebaseFirestore import FirebaseAuth import FirebaseStorage import Nuke struct Sender: SenderType{ var senderId: String var displayName: String init(dic: [String: Any]){ self.senderId = dic["senderId"] as? String ?? "" self.displayName = dic["displayName"] as? String ?? "" } } struct MessageKitMessage: MessageType{ var sender: SenderType var messageId: String let sentDate: Date var kind: MessageKind // init(dic: [String: Any]){ // // self.sender = Sender(dic: dic) // self.messageId = dic["messageId"] as? String ?? "" // self.kind = dic["kind"] as! MessageKind // self.sentDate = dic["senderId"] as! Date // // // } } class MessageKitChatViewController: MessagesViewController, InputBarAccessoryViewDelegate, MessageInputBarDelegate, MessagesDataSource{ var senderUser = Auth.auth().currentUser! var messages = [MessageType]() // let currentUser = Sender(senderId: Auth.auth().currentUser!.uid, displayName: Auth.auth().currentUser!.displayName ?? "Name not found") var passedId: String! var passedDisplayName: String! var currentUser: Sender! = nil var otherUser: Sender! = nil var passedChatroomId: String! override func viewDidLoad() { super.viewDidLoad() messagesCollectionView.messagesDataSource = self messagesCollectionView.messagesLayoutDelegate = self messagesCollectionView.messagesDisplayDelegate = self messageInputBar.delegate = self // otherUser = Sender(senderId: passedId, displayName: passedDisplayName) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) loadBothUsersInfo { self.loadMessages() } } func loadBothUsersInfo(completion: @escaping () -> Void) { guard let currentUserId = Auth.auth().currentUser?.uid else { return } print("currentUserId") print(currentUserId) print("currentUserId") Firestore.firestore().collection("users").document(currentUserId).getDocument { (snapshot, error) in if error != nil{ print(error) }else{ guard let dic = snapshot?.data() else { return } let user = User(dic: dic) let currentUserName = user.userName // self.currentUser = Sender(senderId: currentUserId, displayName: currentUserName) let dic2 = ["senderId": dic["userId"], "displayName": dic["userName"]] self.currentUser = Sender(dic: dic2 as [String : Any]) } } Firestore.firestore().collection("users").document(passedId).getDocument { (snapshot, error) in if error != nil{ print(error) }else{ guard let dic = snapshot?.data() else { return } let user = User(dic: dic) let otherUserName = user.userName let dic2 = ["senderId": dic["userId"], "displayName": dic["userName"]] self.otherUser = Sender(dic: dic2) } } completion() } func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) { Firestore.firestore().collection("chatRooms").document(passedChatroomId).collection("messages").addDocument(data: ["senderId": currentUser.senderId, "displayName": currentUser.displayName, "messageId": "", "sentDate": Timestamp().dateValue(), "kind": text]) { (error) in if error != nil{ print(error) }else{ print("メッセージの保存、送信が完了") inputBar.inputTextView.text = nil self.messagesCollectionView.reloadData() self.messagesCollectionView.scrollToLastItem() } } } func loadMessages(){ Firestore.firestore().collection("chatRooms").document(passedChatroomId).collection("messages").getDocuments { (snapshots, error) in if error != nil{ print(error) }else{ snapshots?.documents.forEach({ (snapshot) in let dic = snapshot.data() let kind = dic["kind"]! let senderId = dic["senderId"]! let sentDate = dic["sentDate"]! as! Timestamp let convertedSentDate = sentDate.dateValue() let displayName = dic["displayName"]! let messageId = dic["messageId"]! let sender = Sender(dic: dic) let dic2 = ["sender": sender, "messageId": dic["messageId"]!, "sentDate": convertedSentDate, "kind": MessageKind.text(dic["kind"] as! String)] // print("'''''''''''''''") // print(sender) // print("'''''''''''''''") // print(kind) // print(senderId) print("dic2") print(dic2) print("dic2") // print(displayName) // print(messageId) self.messages.append(MessageKitMessage(sender: sender, messageId: messageId as! String, sentDate: convertedSentDate, kind: MessageKind.text(dic["kind"] as! String))) self.messagesCollectionView.reloadData() // self.messagesCollectionView.scrollToLastItem() // MessageKitMessage(sender: sender, messageId: dic["messageId"] as! String, sentDate: dic["sentDate"], kind: dic["kind"]) // self.messages.append(MessageKitMessage(dic: dic)) print("messages") print(self.messages) print("messages") // MessageKitMessage(sender: sender, messageId: dic["messageId"] as! String, sentDate: sentDate as! Date, kind: kind) // let sender = Sender(senderId: senderId!, displayName: displayName!) // if senderId != nil { // if displayName != nil{ // let sender = Sender(senderId: senderId!, displayName: displayName!) // } // // } // Sender(senderId: senderId, displayName: displayName) // self.messages.append(MessageKitMessage(dic: dic)) }) } } } func currentSender() -> SenderType { return currentUser } func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { return messages[indexPath.section] } func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { return messages.count } } // //extension MessageKitChatViewController: MessagesDataSource{ // // // func currentSender() -> SenderType { // return currentUser // } // // func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { // print("000000000000000") // print(messages[indexPath.section].sentDate) // print(messages.count) // print("000000000000000") // return messages[indexPath.section] // } // // func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { // return messages.count // } // // // // //} extension MessageKitChatViewController: MessagesLayoutDelegate{ } extension MessageKitChatViewController: MessagesDisplayDelegate{ }
あなたの回答
tips
プレビュー