0

I am trying to make a chat app. I have the login and part of the chat interface done, but when I try to configure the chat with the function collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData , it gives me the error 'Cannot convert return expression of type [JSQMessage] to type JSQMessageData!'

Here is my code for the MessagesViewController :

import UIKit
import JSQMessagesViewController
import Firebase



class MessagesViewController: JSQMessagesViewController {




override func collectionView(collectionView: JSQMessagesCollectionView!,
                             messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
    return message[indexPath.item].userMessages
}

override func collectionView(collectionView: UICollectionView,
                             numberOfItemsInSection section: Int) -> Int {
    return message.count
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = false

    // Do any additional setup after loading the view.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

Here is the code for my custom class Messages:

import Foundation
import JSQMessagesViewController
import Firebase

class Messages {

dynamic var user = FIRAuth.auth()?.currentUser?.uid

dynamic var addedUsers = [String]()

dynamic var userMessages = [JSQMessage]()

dynamic var messageDates = [NSDate]()

}

The array messages is var message = [Messages]()

What is my error, Thank You in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Epic Gamer_1
  • 104
  • 3
  • 12

2 Answers2

0

The issue is that you are trying to pass JSQMessages which you can do if you use JSQMessages instead of your custom class Messages but if you have more variables that you want to keep track of on your message object then you may need your custom implementation.

All You need to have your Messages class conform to the JSQMessageData Protocol. JSQMessages already conform to this which is why they work. I have outlined how I do this previously in another stackoverflow question which you can refer to as a guide if you get stuck. Argument labels do not match any available overloads

Best of luck let me know if there is more I can help you with.

Community
  • 1
  • 1
Dan Leonard
  • 3,325
  • 1
  • 20
  • 32
0

I'm sorry, I solved it myself. I experimented and found that I had to put 'messages[indexPath.item].userMessages[index Path.item]'. I realized that I needed to give the function every single message instead of the array. Thanks to all that helped!

Epic Gamer_1
  • 104
  • 3
  • 12