●やりたいこと
チャットアプリで、まだ開いていないメッセージの数を表示したい。
●試みた方法
配列(複数のkeyとvalueをもつ独自クラス)のうち、filter機能を使って、特定のkeyとvalueの組み合わせの数(=未読のメッセージの数)を数える。
●問題
ちゃんとカウントできていない
以下コードです。
お助け頂けますと幸いです。よろしくお願いいたします!!
Swift
1class New2: NSObject { 2 3 var sender_id: String? 4 5 var timeStamp: NSNumber? 6 7 var toID: String? 8 9 var listened: Bool? //Firebaseのデータベースから取得するこのlistenedフラグで、既読か未読かを区別 10 11} 12
Swift
1 2var news = [New2]() 3var newsDictionary = [String: New2]() 4 5以下viewDidload()内 6Database.database().reference().child("Media_Messages").observe(.childAdded, with: { (snapshot) in 7 if let dictionary = snapshot.value as? [String: AnyObject]{ 8 let new2 = New2() 9 new2.sender_id = dictionary["sender_id"] as? String 10 new2.toID = dictionary["toID"] as? String 11 new2.listened = dictionary["listened"] as? Bool 12 13 //送り主が自分なら、表示する名前とアイコンは、受信者。送り主が自分じゃないなら、表示するのは送信者。 14 if new2.sender_id == Auth.auth().currentUser?.uid { 15 self.newsDictionary[new2.toID!] = new2 16 self.news = Array(self.newsDictionary.values) 17 self.NewTalkCollectionView.reloadData() 18 19 } 20 //送る主が自分以外で、かつ、送り先が自分であるとき 21 else if new2.toID == Auth.auth().currentUser?.uid { 22 self.newsDictionary[new2.sender_id!] = new2 23 self.news = Array(self.newsDictionary.values) 24 self.NewTalkCollectionView.reloadData() 25 } 26 27 28 } 29 }, withCancel: nil)
Swift
1func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 2 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewTalkCollectionViewCell", for: indexPath) as! NewTalkCollectionViewCell 3 4 let user = news[indexPath.row] 5 let numberOfMessages: Int = news.filter{ _ in user.listened == false}.count 6 7 cell.numberOfMessages.text = "(numberOfMessages)" 8
上記コードですと、ちゃんとメッセージ数が1と表示され、開いたら、メッセージ数は0になります。
ただし、未読のメッセージが3つの状態でも、メッセージ数は1と表示され、三つのうち1つでも聞いたら、まだ未読が2つ残っているものの、メッセージ数が0と表示されてしまいます。
Filterのところを以下コードに変えてみたりしましたが、下記のエラーが。
Swift
1 let numberOfMessages: Int = news.filter{ (key: String, value: Bool) -> Bool in return key == "listened" || value == false}.count
Contextual closure type '(New2) -> Bool' expects 1 argument, but 2 were used in closure body
以下10/6追加
Swift
1 2 var numberOfMessages: Int = news.filter {$0.listened == false}.count 3 news.enumerated().forEach { 4 print($0, $1.listened ?? "nil") 5 if let listened = $1.listened { 6 if listened { 7 numberOfMessages += 1 8 } 9 } 10 } 11 print("numberOfMessages =", numberOfMessages)
こちらの出力が、
0 false
numberOfMessages = 1
Swift
1//var numberOfMessages: Int = news.filter {$0.listened == false}.count 2var numberOfMessages = 0 3 news.enumerated().forEach { 4 print($0, $1.listened ?? "nil") 5 if let listened = $1.listened { 6 if listened { 7 numberOfMessages += 1 8 } 9 } 10 } 11 print("numberOfMessages =", numberOfMessages)
こちらの出力が、
0 false
numberOfMessages = 0
です。両者とも、開いていないメッセージ(listened = false)が複数ある状態です。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/01 15:57
2018/10/02 00:29
2018/10/03 11:50
2018/10/04 00:26
2018/10/06 07:57
2018/10/09 00:18