WatchKit has a presenter method that presents a QuickReplyButton with predetermined text replies and those replies can be sent to a UI element such as a label in a cell/row.
I have inserted the row into the last position however each subsequent call inserts the new reply above the previous reply not below.
I attempted to append the text selection choice to the end of the chat model's message array using self.chats.append(text as AnyObject)
, but the choice is a string and I am unable to append a string to the chat model array. Value of type 'String' does not conform to 'AnyObject'
. If I completely omit self.chats.append(text as AnyObject)
it works as shown.
ChatDetailController
// ...
// start of @IBAction
// ...
self.chats.append(text as AnyObject) // how do I write this?
let newIndex = self.chats.count - 1
//...
})
Model
public struct ChatModel: Codable {
public var message: String
// more properties
enum CodingKeys: String, CodingKey {
case message = "messageText"
// more properties
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.message = try container.decode(String.self, forKey: .message)
// more properties
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.message, forKey: .message)
// more properties
}
}
json
[
{
"fromId": "wx1234skjksmsjdfwe234",
"toId": "wx43922sdkfjsfmmxdfmx",
"messageText": "Have you hopped on the bus Gus?",
"imageUrl": "https://i.imgur.com/PJcyle7.jpg",
"read": "true"
},
// more objects.
]
I have update your ChatModel. Use this model struct. You can init from json decoder or from your run time values.
public struct ChatModel: Codable {
public var message: String
public var fromId: String
public var toId: String
public var imageUrl: URL?
enum CodingKeys: String, CodingKey {
case message = "messageText"
case fromId = "fromId"
case toId = "toId"
case imageUrl = "imageUrl"
//case read = "read"
}
init (message:String , fromId:String, toID : String, imgUrl : URL?) {
self.message = message
self.fromId = fromId
self.toId = toID
self.imageUrl = imgUrl
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.message = try container.decode(String.self, forKey: .message)
self.fromId = try container.decode(String.self, forKey: .fromId)
self.toId = try container.decode(String.self, forKey: .toId)
self.imageUrl = try container.decode(URL.self, forKey: .imageUrl)
//self.read = try container.decode(Bool.self, forKey: .read)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.message, forKey: .message)
try container.encode(self.fromId, forKey: .fromId)
try container.encode(self.toId, forKey: .toId)
try container.encode(self.imageUrl, forKey: .imageUrl)
//try container.encode(self.read, forKey: .read)
}
}
And this is how you can create chatModel object to inset in chats array
Note: Use same fromID and ToID you have received from json
let newChat = ChatModel(message: "test", fromId: "fromID", toID: "tOID", imgUrl: nil)
self.chats.append(newChat)
Now insert row in table at end