gpt4 book ai didi

ios - 按收到日期的顺序显示两个自定义对象数组

转载 作者:搜寻专家 更新时间:2023-11-01 06:37:52 25 4
gpt4 key购买 nike

我有两个自定义对象数组([InboundMessages]、[OutboundMessages])

    struct InboundMessage {

let senderName: String!
let senderText: String!
let senderProfileImageName: String!
let timeAndDateMessageReceived: NSDate!
let hasBeenRead: Bool!
let messageUUID = NSUUID().UUIDString
let conversationUUID: NSUUID = NSUUID()

}
struct OutboundMessage {

var replyText: String!
var messageUUID = NSUUID().UUIDString
var timeAndDateMessageSent: NSDate!
var conversationUUID: NSUUID = NSUUID()
}

与另一个自定义对象“对话”一起保存

    class Conversation {

var uUID: NSUUID!
var inboundMessages: [InboundMessage] = []
var outboundMessages:[OutboundMessage] = []

}

我想像普通对话一样在 tableview 中显示按日期排序的对话,但在多次尝试后我无法理解如何去做。我觉得最好将它们全部放入一个排序数组中,然后让 tableview 处理它,但我仍然不确定如何对它们进行排序,然后如何将两个不同的自定义对象放入同一个数组中?

您可能已经猜到我是 swift 和 iOS 的新手,因此非常感谢您的帮助。

谢谢

最佳答案

<强>1。创建基类:

class Message {
let timeAndDateMessageReceived: NSDate!
let hasBeenRead: Bool!
let messageUUID = NSUUID().UUIDString
let conversationUUID: NSUUID = NSUUID()
}

<强>2。扩展 Message 类:

class OutboundMessage: Message
class InboundMessage: Message

<强>3。创建一个 Message 数组:

var messages: [Message] = inboundMessages as [Message] + outboundMessages as [Message]

<强>4。按 timeAndDateMessageReceived 排序:

messages.sort({ $0.timeAndDateMessageReceived.compare($1. timeAndDateMessageReceived) == NSComparisonResult.OrderedAscending })

关于ios - 按收到日期的顺序显示两个自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com