###前提・実現したいこと
http://www.atmarkit.co.jp/ait/articles/1606/06/news020_3.html
上記のページを参考にしてswift3とFirebaseを使ってリアルタイムチャットアプリを作ろうとしています。
ViewContollor.swiftをswift3に対応させようと書き換えたのですが、
Firebaseのデータベースへ書き込みができないのでそれを解決したいと思い質問させていただきました。
cocoapodsを使ってFirebaseとJSQMessageViewControllerを導入し、
シミュレーターのキーボードからFirebaseのデータベースへ書き込みを行おうとしたところエラーが出てしまいました。
googleで調べたところ、ストーリーボードとコードの接続がうまくいっていない時にこのエラーが起こるようなので、
みてみたのですが、ストーリーボードには何もありませんでした。
どうか、ご助力をお願いいたします。
いつもありがとうございます。
PS:ご教授いただいた解決方法を質問の一番下に書いてあります。2016 11/2
###発生している問題・エラーメッセージ
Thread 1: signal SIGABRT
###該当のソースコード
AppDelegate
import UIKit import Firebase @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { FIRApp.configure() return true } func applicationWillResignActive(_ application: UIApplication) { } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } func applicationDidBecomeActive(_ application: UIApplication) { } func applicationWillTerminate(_ application: UIApplication) { } }
ViewControllerSwift
Swift3.0
1import UIKit 2import JSQMessagesViewController 3import Firebase 4import FirebaseDatabase 5 6 7class ViewController: JSQMessagesViewController { 8 var messages: [JSQMessage] = [ 9 JSQMessage(senderId: "Dummy", displayName: "A", text: "こんにちは!"), 10 JSQMessage(senderId: "Dummy2", displayName: "B", text: "こんにちは♪") 11 ] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 senderDisplayName = "A" 17 senderId = "Dummy" 18 } 19 20 21 override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData { 22 return messages[indexPath.item] 23 } 24 25 26 override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath) -> JSQMessageBubbleImageDataSource? { 27 if messages[indexPath.row].senderId == senderId { 28 return JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImage( 29 with: UIColor(red: 112/255, green: 192/255, blue: 75/255, alpha: 1)) 30 } else { 31 return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImage( 32 with: UIColor(red: 229/255, green: 229/255, blue: 229/255, alpha: 1)) 33 } 34 } 35 36 //cellForItemAtIndexPathのIndexPathを削除、whiteColor()、grayColor()のColor()を削除。 37 38 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 39 40 let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as? JSQMessagesCollectionViewCell 41 if messages[indexPath.row].senderId == senderId { 42 cell?.textView?.textColor = UIColor.white 43 } else { 44 cell?.textView?.textColor = UIColor.darkGray 45 } 46 return cell! 47 } 48 49 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 50 return messages.count 51 } 52 53 54 55 override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath) -> JSQMessageAvatarImageDataSource? { 56 57 return JSQMessagesAvatarImageFactory.avatarImage( 58 withUserInitials: messages[indexPath.row].senderDisplayName, 59 backgroundColor: UIColor.lightGray, textColor: UIColor.white, 60 font: UIFont.systemFont(ofSize: 10), diameter: 30) 61 } 62 63 64 65 66 override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) { 67 inputToolbar.contentView.textView.text = "" 68 let ref = FIRDatabase.database().reference() 69 ref.child("messages").childByAutoId().setValue( 70 ["senderId": senderId, "text": text, "displayName": senderDisplayName]) 71 } 72} 73
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3 Xcode8 iOS10
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。