1

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.

]
Edison
  • 11,881
  • 5
  • 42
  • 50

1 Answers1

2

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

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
  • Thank you. I have some some questions. **1** Why does any string work in `ChatModel(message: "any string works here."`. **2 ** So when the `presentInputText Controller` is used, the choice selection becomes a new value of the struct's message key? – Edison Apr 02 '19 at 18:50
  • Please do not change the question content (title), as it may create problem in some other one's understanding. My answer was in this context that you have to add a new row with proper chat object. And it helped you out. For your next questions you may ask in new thread. – M Zubair Shamshad Apr 03 '19 at 05:19
  • Title is more explicit now. `Updating the count of an object's keys by appending a string `. Your answer helps me to access `self.chats` and append to it. Appending then *updates the count*. It's more explicit than the previous title. It's the same question. I also trimmed the content because the rest is unnecessary. If you like, I can change the title for you. Would you like me to edit it? – Edison Apr 03 '19 at 05:25