1

I am working on a messaging app using JSQMessagesViewController, and I want to associate another variable, an Integer score, with each message (along with the defaults such as senderID, text, and displayName). This is how I attempt to implement this:

func addMessage(id: String, text: String, displayName: String, score: Int) {

    // Make sure the character count is between 10 and 140, then add message to message list to display
    if (text.characters.count <= 10 || text.characters.count >= 140) {

    }
    else {
        let message = JSQMessage(senderId: id, displayName: displayName, text: text, score: score)
        messages.append(message)
    }

}

However I am getting the following error message:

Argument labels '(senderId:, displayName:, text:, score:)' do not match any available overloads

I can only assume that this is because there is some pre-set definition as to what data the JSQMessage object can hold, I am just unsure how to override it so that I can associate an additional variable with my messages.

Note: I tried to directly change the code in the JSQMessagesViewController framework itself (within the JSQMessage.h and JSQMessage.m files) to add an extra variable but this just causes more errors and I am afraid I will mess something up.

Any solutions?

Thanks in advance.

EDIT

I think I figured out how to do it!

import UIKit
import JSQMessagesViewController

class CustomMessage: JSQMessage {
    var score : Int

    init(senderId:String, displayName:String, text:String, score:Int) {
        self.score = score
        super.init(senderId:senderId, displayName:displayName, text:text)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

However I am now getting an error after super.init as such: must call a designated initializer of the superclass 'JSQMessage'

1
  • hii i also want to add another paramter for message id @Ahad Sheriff Aug 4, 2018 at 5:06

1 Answer 1

6

Just subclass the JSQMessage object and add your extra variable to your subclassed object. Then it will conform and have all the same methods.

Edit:

It looks like I might have miss lead you, the documentation instructs you to conform to the JSQMessageData protocol. I did it like this

class Message: NSObject, JSQMessageData {
var text_: String?
var senderId_: String?
var date_: NSDate?
var senderDisplayName_: String?
var isMediaMessage: Bool?
var score: Int?               ***** Here is your new Variable

init(text: String?, senderId: String?, senderDisplayName: String?, score: Int?, date: NSDate) {
    self.text_ = text
    self.senderId_ = senderId
    self.isOutBound_ = isOutBound
    self.date_ = date
    self.senderDisplayName_ = senderDisplayName
    self.score_ = score       *****
}

func text() -> String? {
    return text_
}
func score() -> Int? {        *****
    return score_
}

func senderId() -> String? {
    return senderId_
}

func date() -> NSDate? {
    return date_
}

func senderDisplayName() -> String? {
    return senderDisplayName_
}

func isMediaMessage() -> Bool {
    return isMediaMessage_
}

func messageHash() -> UInt {
    return UInt(self.hash)
}

}

You can still do it the other way but Protocols are the way to go.

Let me know if that helped I may have just confused you more :) But I will try and clarify. Good Luck

4
  • Could you check out the edit I made to my question? I think I almost got it, I am just getting a tiny error. Thanks for the awesome help by the way you're great! Aug 11, 2016 at 17:10
  • Hey just checking if you saw my edit and if that helped. Aug 13, 2016 at 15:48
  • i want to add messageID property to JSQMessage from which class i would change and add a messageID property in objective c class instaled after pod Aug 4, 2018 at 5:27
  • @DanielLeonard pls reply Aug 8, 2018 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.