如何为符合 JSQMessageData 的自定义消息在 swift 3 中创建 class?

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

我不熟悉Objective-C所以我无法理解这一点。 我的消息结构与 JSQMessage 不同,所以我想创建自己的 class.

这是我的 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?

}

如果您在 Swift 中实现它,并且您想让它在该框架内可用,您应该使您的 class 符合 JQMessageData。您只需要实现所需的方法和变量。

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
    // ...

}

检查 documentation 在这些方法中要 return 的内容。