Ahad Sheriff Ahad Sheriff - 3 months ago 68
Swift Question

Argument labels do not match any available overloads

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'

Answer

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

Comments