arrays - swift 过滤器数组产生重复项(如何删除过滤器重复项)

标签 arrays swift date struct filter

我使用从 Firebase 数据库检索的数据创建了按日期过滤的消息。我的目标是为相应的消息组制作标题部分。

struct dateModelStructure {
        var date: String
        var contents: String
    }
var messagesPerDateDictionary: [dateModelStructure] = []
var dateSection = [Any]()
var array = [Any]()
array = self.messagesPerDateDictionary.filter { $0.date == chat.datestampString()}
self.dateSection.append(array)

问题是,如果我将数组附加到 dateSection,它会附加整个重复数据。例如,

[
[ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?")], 
[ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?")], 
[ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Thank you")], 
[ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20")], 
[ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20"), ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")], 
[ChatViewController.dateModelStructure(date: "2019-08-15", contents: "Counts")]
]

如果我只是想要这样的东西,我该怎么办。

[ 
[ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Thank you")],  
[ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20"), ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")], 
[ChatViewController.dateModelStructure(date: "2019-08-15", contents: "Counts")]
]

如果我的问题不够清楚,我深表歉意。 谢谢您的帮助!

最佳答案

这是我在 Playground 上想到的。我希望我正确理解你的问题:

class ChatViewController {

    struct dateModelStructure: Hashable {
        var date: String
        var contents: String

        static func ==(lhs: ChatViewController.dateModelStructure, rhs: ChatViewController.dateModelStructure) -> Bool {
            return lhs.date == rhs.date && lhs.contents == rhs.contents
        }
    }

    var messagesPerDateDictionary: [dateModelStructure] = []
    var dateSection = [Any]()
}

var someData = [
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?")],
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"),ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?")],
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Thank you")],
    [ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20")],
    [ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20"), ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")],
    [ChatViewController.dateModelStructure(date: "2019-08-15", contents: "Counts")]
]

var helperDict = [String: [ChatViewController.dateModelStructure]]()

someData.forEach { (data) in
    guard let firstDate = data.first?.date else {
        return
    }
    if let existingArray = helperDict[firstDate] {
        helperDict[firstDate] = existingArray + data
    } else {
        helperDict[firstDate] = data
    }
}

var filtered : [[ChatViewController.dateModelStructure]] = helperDict.values.reduce([[ChatViewController.dateModelStructure]]()) { (previous, current) -> [[ChatViewController.dateModelStructure]] in
    var next = [Array(Set(current))] + previous
    return next
}



filtered.forEach { (items) in
    print ("------------------------------")
    items.forEach({ (item) in
        print(item)
    })
}

------------------------------
dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20")
dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")
------------------------------
dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?")
dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?")
dateModelStructure(date: "2019-08-13", contents: "Thank you")
------------------------------
dateModelStructure(date: "2019-08-15", contents: "Counts")

此代码可以改进,但请告诉我它是否为您提供了您正在搜索的结果。

关于arrays - swift 过滤器数组产生重复项(如何删除过滤器重复项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57520319/

相关文章:

MySQL : Select count group by day with custom starting and ending hours for a day

c - 复制数组并修改其元素的函数

c++ - 队列数组指针 (C++)

php - 在 PHP 中转换 key 的最佳方法

ios - 如何在 Swift 中实现 GMUClusterRenderer

ios - 从最后一个已知邮件 ID 获取最新邮件 - Mailcore

python - 如何从一年中的一周计算出一个月中的一周?

php - 多维数组有条件删除重复键值

Swift Singleton 用于在委托(delegate)中预加载纹理而不将数据传输到其他类

python - 如何在Bokeh DateRangeSlider中实现1个月 `step`?