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'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…