通过附加字符串来更新对象键的计数

问题描述 投票:1回答:1

WatchKit有一个presenter方法,它提供带有预定文本回复的QuickReplyButton,并且这些回复可以发送到UI元素,例如单元格/行中的标签。

我已将行插入最后一个位置,但每个后续调用都会在上一个回复之上插入新回复而不是下方。

我尝试使用self.chats.append(text as AnyObject)将文本选择选项附加到聊天模型的消息数组的末尾,但是选择是一个字符串,我无法将字符串附加到聊天模型数组。 Value of type 'String' does not conform to 'AnyObject'。如果我完全省略self.chats.append(text as AnyObject)它的工作原理如图所示。

ChatDetailController

// ...
// start of @IBAction
// ...

    self.chats.append(text as AnyObject) // how do I write this?
    let newIndex = self.chats.count - 1
    //...

    })

模型

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.

]
swift watchkit
1个回答
2
投票

我已经更新了你的ChatModel。使用此模型结构。您可以从json解码器或运行时值启动。

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)
}

}

这就是你如何创建chatModel对象以插入聊天数组

注意:使用从json收到的相同的fromID和ToID

    let newChat = ChatModel(message: "test", fromId: "fromID", toID: "tOID", imgUrl: nil)
    self.chats.append(newChat)

现在在表格中插入行

© www.soinside.com 2019 - 2024. All rights reserved.