Swift 附加到带有自定义对象的数组并对其进行排序

Swift appending to an array with custom objects and sorting them

我正在制作一个集合视图,向某人显示他们的 'feed',其中包括他们的赞美、喜欢、回复和发送。我有一个名为 Feed 的对象,它由一组赞美、一组喜欢、一组回复和一组发送组成。这些较小数组中的每个对象都有一个 date_created 属性。

我知道如何根据一个按日期排序的数组来填充集合视图,但是如何根据发送日期从四个数组中加载每个点赞、回复、赞美和发送的集合视图?

我是否可以将这些对象中的每一个附加到一个数组中?如果是这样,我该如何遍历该数组并对其中的所有内容进行排序?

struct Feed: Decodable {
    let likes: [Like]
    let replies: [Reply]
    let compliments: [Compliment]
    let sent: [Compliment]
}

struct Like: Decodable {
    let id: Int
    let user: QuickUser
    let created_at: String
}

struct Reply: Decodable {
    let parent: Compliment
    let content: String
    let created_at: String
}

struct Compliment: Decodable {
    let id: Int
    let content: String
    let sender: QuickUser

    let created_at: String

    let is_liked: Bool
    let recipient: QuickUser
    let is_public: Bool
}

func getFeed() {
    UserServices.getInboxFeed { feed in
        guard let feed = feed else {
            self.showOkayAlert(title: "Oops!", message: "We are having trouble loading your inbox.", completion: nil)
            return
        }
        self.feed = feed

        DispatchQueue.main.async {
            //load collection view here
        }
    }
}

集合视图的模型

好吧,为了解决这个问题,我制作了一个虚拟协议,其中包含它们共享的共享属性。

在这种情况下是 created_at,我认为这应该是日期格式。

但是下一步是对 Feed 进行扩展,其中包含 function

那个 return 是向我调用的虚拟 protocol 确认的最新对象,它 LatestAction

它基本上按 date 和 return 第一项对每个数组进行排序。

在我们的案例中,最新的 date

之后创建一个 [LatestAction] 以相同的方式再次排序,return 第一项是 LatestAction

观察下面的代码。

struct QuickUser: Codable {

}

struct Feed: Decodable {
    let likes: [Like]
    let replies: [Reply]
    let compliments: [Compliment]
    let sent: [Compliment]
}
extension Feed {
    func latestAction() -> LatestAction {
        let latestLike = self.likes.sorted(by: { [=10=].created_at.compare(.created_at) == .orderedDescending }).first!
        let latestReply = self.replies.sorted(by: { [=10=].created_at.compare(.created_at) == .orderedDescending }).first!
        let latestComp = self.compliments.sorted(by: { [=10=].created_at.compare(.created_at) == .orderedDescending }).first!
        let latestActions: [LatestAction] = [latestReply, latestLike, latestComp].sorted(by: { [=10=].created_at.compare(.created_at) == .orderedDescending })
        return latestActions.first!
    }

}
protocol LatestAction  {
    var created_at: Date { get }
}

struct Like: Decodable, LatestAction {

    let id: Int
    let user: QuickUser
    let created_at: Date
}

struct Reply: Decodable, LatestAction {

    let parent: Compliment
    let content: String
    let created_at: Date
}

struct Compliment: Decodable, LatestAction {

    let id: Int
    let content: String
    let sender: QuickUser

    let created_at: Date

    let is_liked: Bool
    let recipient: QuickUser
    let is_public: Bool
}



// Simply used like this
var latest = feed.latestAction() 

我们还没有完成,如果您检查了 latest 类型,它会说 LatestAction 并且只能访问那里的 属性,即 created_at

现在,对于最后一部分,我们将 TypeCast Object

if let latest = feed.latestAction() as? Like {
    // used as Like
} else if let latest = feed.latestAction() as? Compliment {
    // used as Compliment
} else if let latest = feed.latestAction() as? Reply {
    // used as reply
}