質問編集履歴
1
タイトル修正、コードの追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
一度は表示されますが再びロードするとUnexpectedly found nilとなります
|
body
CHANGED
@@ -16,9 +16,21 @@
|
|
16
16
|
self.sentDate = dic["senderId"] as! Date
|
17
17
|
```
|
18
18
|
|
19
|
-
### 該当のソースコード
|
20
19
|
|
20
|
+
|
21
|
+
### 試したこと
|
22
|
+
|
23
|
+
as以降の部分にいろんなものを入れたり試したりしました。
|
24
|
+
他にはinit丸々無くしてやろうとしたのですがオプショナル?の問題で上手くいきませんでした。
|
25
|
+
|
26
|
+
用語の定義が自分の中でイマイチ理解できていない部分があり説明が上手くできません。ごめんなさい。
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
### 一部動くコード
|
32
|
+
|
21
|
-
```ここに言語
|
33
|
+
```ここに言語を入力
|
22
34
|
import UIKit
|
23
35
|
import MessageKit
|
24
36
|
import MessageInputBar
|
@@ -50,21 +62,23 @@
|
|
50
62
|
let sentDate: Date
|
51
63
|
var kind: MessageKind
|
52
64
|
|
53
|
-
init(dic: [String: Any]){
|
65
|
+
// init(dic: [String: Any]){
|
54
|
-
|
66
|
+
//
|
55
|
-
self.sender = Sender(dic: dic)
|
67
|
+
// self.sender = Sender(dic: dic)
|
56
|
-
self.messageId = dic["messageId"] as? String ?? ""
|
68
|
+
// self.messageId = dic["messageId"] as? String ?? ""
|
57
|
-
self.kind = dic["kind"] as! MessageKind
|
58
|
-
self.sentDate = dic["senderId"] as! Date
|
59
|
-
// self.kind = dic["
|
69
|
+
// self.kind = dic["kind"] as! MessageKind
|
60
|
-
|
70
|
+
// self.sentDate = dic["senderId"] as! Date
|
71
|
+
//
|
72
|
+
//
|
61
|
-
}
|
73
|
+
// }
|
62
74
|
|
63
75
|
}
|
64
76
|
|
65
77
|
|
66
|
-
class MessageKitChatViewController: MessagesViewController, InputBarAccessoryViewDelegate, MessageInputBarDelegate{
|
78
|
+
class MessageKitChatViewController: MessagesViewController, InputBarAccessoryViewDelegate, MessageInputBarDelegate, MessagesDataSource{
|
67
79
|
|
80
|
+
|
81
|
+
|
68
82
|
var senderUser = Auth.auth().currentUser!
|
69
83
|
var messages = [MessageType]()
|
70
84
|
|
@@ -90,6 +104,23 @@
|
|
90
104
|
messagesCollectionView.messagesDisplayDelegate = self
|
91
105
|
messageInputBar.delegate = self
|
92
106
|
|
107
|
+
|
108
|
+
// otherUser = Sender(senderId: passedId, displayName: passedDisplayName)
|
109
|
+
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
override func viewWillAppear(_ animated: Bool) {
|
115
|
+
super.viewWillAppear(animated)
|
116
|
+
|
117
|
+
|
118
|
+
loadBothUsersInfo {
|
119
|
+
self.loadMessages()
|
120
|
+
}
|
121
|
+
|
122
|
+
}
|
123
|
+
func loadBothUsersInfo(completion: @escaping () -> Void) {
|
93
124
|
guard let currentUserId = Auth.auth().currentUser?.uid else { return }
|
94
125
|
print("currentUserId")
|
95
126
|
print(currentUserId)
|
@@ -124,10 +155,7 @@
|
|
124
155
|
|
125
156
|
}
|
126
157
|
}
|
127
|
-
// otherUser = Sender(senderId: passedId, displayName: passedDisplayName)
|
128
|
-
|
129
|
-
|
130
|
-
|
158
|
+
completion()
|
131
159
|
}
|
132
160
|
|
133
161
|
|
@@ -140,6 +168,10 @@
|
|
140
168
|
}else{
|
141
169
|
print("メッセージの保存、送信が完了")
|
142
170
|
inputBar.inputTextView.text = nil
|
171
|
+
self.messagesCollectionView.reloadData()
|
172
|
+
|
173
|
+
self.messagesCollectionView.scrollToLastItem()
|
174
|
+
|
143
175
|
}
|
144
176
|
}
|
145
177
|
|
@@ -153,30 +185,41 @@
|
|
153
185
|
}else{
|
154
186
|
snapshots?.documents.forEach({ (snapshot) in
|
155
187
|
|
188
|
+
|
156
189
|
let dic = snapshot.data()
|
157
190
|
|
158
|
-
let kind = dic["kind"]
|
191
|
+
let kind = dic["kind"]!
|
159
|
-
let senderId = dic["senderId"]
|
192
|
+
let senderId = dic["senderId"]!
|
160
|
-
let sentDate = dic["sentDate"]
|
193
|
+
let sentDate = dic["sentDate"]! as! Timestamp
|
194
|
+
let convertedSentDate = sentDate.dateValue()
|
161
|
-
let displayName = dic["displayName"]
|
195
|
+
let displayName = dic["displayName"]!
|
162
|
-
let messageId = dic["messageId"]
|
196
|
+
let messageId = dic["messageId"]!
|
163
197
|
|
164
198
|
let sender = Sender(dic: dic)
|
165
|
-
print("'''''''''''''''")
|
166
|
-
print(sender)
|
167
|
-
print("'''''''''''''''")
|
168
|
-
print(kind)
|
169
|
-
print(senderId)
|
170
|
-
print(sentDate)
|
171
|
-
print(displayName)
|
172
|
-
print(messageId)
|
173
199
|
|
200
|
+
let dic2 = ["sender": sender, "messageId": dic["messageId"]!, "sentDate": convertedSentDate, "kind": MessageKind.text(dic["kind"] as! String)]
|
201
|
+
// print("'''''''''''''''")
|
202
|
+
// print(sender)
|
203
|
+
// print("'''''''''''''''")
|
204
|
+
// print(kind)
|
205
|
+
// print(senderId)
|
206
|
+
print("dic2")
|
207
|
+
print(dic2)
|
208
|
+
print("dic2")
|
209
|
+
// print(displayName)
|
210
|
+
// print(messageId)
|
211
|
+
self.messages.append(MessageKitMessage(sender: sender, messageId: messageId as! String, sentDate: convertedSentDate, kind: MessageKind.text(dic["kind"] as! String)))
|
212
|
+
self.messagesCollectionView.reloadData()
|
213
|
+
// self.messagesCollectionView.scrollToLastItem()
|
214
|
+
|
215
|
+
// MessageKitMessage(sender: sender, messageId: dic["messageId"] as! String, sentDate: dic["sentDate"], kind: dic["kind"])
|
216
|
+
|
174
|
-
self.messages.append(MessageKitMessage(dic: dic))
|
217
|
+
// self.messages.append(MessageKitMessage(dic: dic))
|
175
218
|
print("messages")
|
176
219
|
print(self.messages)
|
177
220
|
print("messages")
|
178
|
-
// MessageKitMessage(sender: sender, messageId: messageId, sentDate: sentDate, kind: kind)
|
221
|
+
// MessageKitMessage(sender: sender, messageId: dic["messageId"] as! String, sentDate: sentDate as! Date, kind: kind)
|
179
|
-
|
222
|
+
|
180
223
|
// let sender = Sender(senderId: senderId!, displayName: displayName!)
|
181
224
|
// if senderId != nil {
|
182
225
|
// if displayName != nil{
|
@@ -197,33 +240,50 @@
|
|
197
240
|
}
|
198
241
|
}
|
199
242
|
|
200
|
-
|
201
243
|
|
244
|
+
func currentSender() -> SenderType {
|
245
|
+
return currentUser
|
202
|
-
}
|
246
|
+
}
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
extension MessageKitChatViewController: MessagesDataSource{
|
207
247
|
|
248
|
+
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
|
249
|
+
|
250
|
+
return messages[indexPath.section]
|
251
|
+
}
|
208
252
|
|
209
|
-
func
|
253
|
+
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
|
210
|
-
|
254
|
+
return messages.count
|
211
|
-
|
255
|
+
}
|
212
256
|
|
213
|
-
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
|
214
|
-
return messages[indexPath.section]
|
215
|
-
}
|
216
|
-
|
217
|
-
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
|
218
|
-
return messages.count
|
219
|
-
}
|
220
257
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
258
|
}
|
225
259
|
|
226
260
|
|
261
|
+
//
|
262
|
+
//extension MessageKitChatViewController: MessagesDataSource{
|
263
|
+
//
|
264
|
+
//
|
265
|
+
// func currentSender() -> SenderType {
|
266
|
+
// return currentUser
|
267
|
+
// }
|
268
|
+
//
|
269
|
+
// func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
|
270
|
+
// print("000000000000000")
|
271
|
+
// print(messages[indexPath.section].sentDate)
|
272
|
+
// print(messages.count)
|
273
|
+
// print("000000000000000")
|
274
|
+
// return messages[indexPath.section]
|
275
|
+
// }
|
276
|
+
//
|
277
|
+
// func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
|
278
|
+
// return messages.count
|
279
|
+
// }
|
280
|
+
//
|
281
|
+
//
|
282
|
+
//
|
283
|
+
//
|
284
|
+
//}
|
285
|
+
|
286
|
+
|
227
287
|
extension MessageKitChatViewController: MessagesLayoutDelegate{
|
228
288
|
|
229
289
|
|
@@ -235,9 +295,15 @@
|
|
235
295
|
}
|
236
296
|
```
|
237
297
|
|
238
|
-
|
298
|
+
上の一部動くコードはチャットルームをクリックした後しっかり動作しチャットメッセージがロードされ表示されるのですが、その後戻るボタンを押してチャットルーム一覧に戻り、同じ部屋をクリックすると
|
239
299
|
|
300
|
+
```ここに言語を入力
|
301
|
+
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file
|
302
|
+
```
|
303
|
+
というエラーが
|
304
|
+
```ここに言語を入力
|
240
|
-
|
305
|
+
func currentSender() -> SenderType {
|
306
|
+
return currentUser
|
307
|
+
|
308
|
+
```
|
241
|
-
|
309
|
+
のreturn currentUserに対して表示され原因が分かりません。
|
242
|
-
|
243
|
-
用語の定義が自分の中でイマイチ理解できていない部分があり説明が上手くできません。ごめんなさい。
|