閲覧ありがとうございます。
現在、Swift4 Xconde9.2 Firebaseを用いてアプリを作成しています。
みなさまにお聞きしたいことは以下の通りです。
まだSwiftに慣れておらず、質問が分かりづらければすみません。。
>やりたいこと
TableView上にFirebaseからSnapShot内の配列のデータを読み込み、表示させることです。
現状は以下のようにコーティングしています。
>問題
Firebase内に格納されているデータに、配列があります。
これは以下のようなチェックリストを作成するためです。
ですが、コードを実行すると以下のエラーがててしまいます。
Could not cast value of type '__NSArrayM' (0x112f4ab68) to 'NSDictionary' (0x112f4c288).
Could not cast value of type '__NSCFNumber' (0x10def82e8) to 'NSDictionary' (0x10def9288).
2018-05-27 16:20:58.899536+0900 TopPage[26249:1524059] Could not cast value of type '__NSCFNumber' (0x10def82e8) to 'NSDictionary' (0x10def9288).
どうにかしてCollectionViewに表示させる手段はありませんしょうか?
Swift
1//データスナップショットを入れる空の配列 2 var contentArray: [DataSnapshot] = [] 3 4//readUsersメソッドにてリスナを作成し、reloadメソッドを読み込む 5 func readUsers() { 6 7 ref.child("Users").child(uid).observe(.value, with: {(snapShots) in 8 if snapShots.children.allObjects is [DataSnapshot] { 9 10 print("snapShot.children...(snapShots.childrenCount)") 11 print("snapShot...(snapShots)") 12 13 self.reload(snap: snapShots) 14 } 15 }) 16 } 17 18//配列contentArrayにのリスナでセットしたDanaSnapShotデータを入れる 19 func reload(snap: DataSnapshot) { 20 if snap.exists() { 21 print(snap) 22 23 contentArray.removeAll() 24 25 for item in snap.children { 26 contentArray.append(item as! DataSnapshot) 27 } 28 29 ref.child("Projects").keepSynced(true) 30 31 collectionView.reloadData() 32 } 33 } 34 35//CollectionView上に表示させるメソッド内で、Arrayを辞書型にキャスト 36 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 37 let viewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TopCollectionViewCell 38 39 print("cellForRowAt内の...(contentArray)") 40 let item = contentArray[indexPath.row] 41 42 let content = item.value as! Dictionary <String, AnyObject> 43 print(content) 44 45 viewCell.pjTitle.text = String(describing: content["PjTitle"]!) 46 47 return viewCell 48 } 49 50 51
あなたの回答
tips
プレビュー