Search code examples
jsqmessagesviewcontroller

How can I create class in swift 3 for my custom message that conforms to JSQMessageData?


I am not familiar with Objective-C so I am not able to understand this. My message structure is different than JSQMessage so I want to create my own class.

Here is my class

class ChatMessage:NSObject {

var createdAt:Double?
var createdName:String?
var createdUid:String?
var imageUrl:String?
var name:String?
var text:String?
var uColor:String?
var uid:String?

}


Solution

  • If you're implementing this in Swift, and you want to make it usable within that framework, you should make your class conform to JQMessageData. You just need to implement the required methods and variables.

    class ChatMessages: NSObject, JSQMessageData {
    
        // MARK: Required methods
        func senderId() -> String! {
            // your code here, return a unique ID for your sender
        }
    
        func senderDisplayName() -> String! {
            // your code here, return the display name of your sender
        }
    
        func date() -> Date! {
            // your code here, return your message date
        }
    
        func isMediaMessage() -> Bool {
            // your code here, return whether your message contains media
        }
    
        func messageHash() -> UInt {
            // your code here, return a unique identifier for your message
        }
    
        // MARK: Optional methods
    
        func text() -> String! {
            // your code here, return your message text
        }
    
        func media() -> JSQMessageMediaData! {
            // your code here, return your message media if required
        }
    
        // MARK: Other methods and variables
        // ...
    
    }
    

    Check the documentation for what to return in those methods.