快速过滤器数组产生重复项(如何删除过滤器重复项)

时间:2019-08-16 07:10:01

标签: 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")]
]

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

1 个答案:

答案 0 :(得分:0)

这是我在操场上上来的东西。希望我能正确理解您的问题:

Index.cshtml

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