快速添加到具有自定义对象的数组并对其进行排序

时间:2018-11-05 00:54:54

标签: arrays swift sorting custom-object

我正在制作一个集合视图,向某人显示他们的“提要”,其中包括他们的赞美,喜欢,答复和发送。我有一个名为Feed的对象,该对象由一个赞美数组,一个点赞数组和一个回复数组以及一个send数组组成。这些较小数组中的每个对象都具有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
        }
    }
}

集合视图的样机 mockup

1 个答案:

答案 0 :(得分:0)

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

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

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

返回确认的最新对象给我调用的虚拟protocol,它是LatestAction

它基本上按date对每个数组进行排序,并返回第一项。

在我们的例子中是最新的date

之后,将创建一个[LatestAction],以相同的方式再次对其进行排序,并返回第一项,即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: { $0.created_at.compare($1.created_at) == .orderedDescending }).first!
        let latestReply = self.replies.sorted(by: { $0.created_at.compare($1.created_at) == .orderedDescending }).first!
        let latestComp = self.compliments.sorted(by: { $0.created_at.compare($1.created_at) == .orderedDescending }).first!
        let latestActions: [LatestAction] = [latestReply, latestLike, latestComp].sorted(by: { $0.created_at.compare($1.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
}