【问题标题】:Grouping appointments by day in Swift在 Swift 中按天分组约会
【发布时间】:2025-12-28 10:45:06
【问题描述】:

我有一个日期字符串数组(见下文)。我想将此数组用于我的表格视图,以显示按天分类的时间(类似于 iOS 联系人应用程序中的联系人表格,按字母分类)。

作为第一步,我想将日期字符串数组分组以实现分类约会时间数组(见下文),请告知我如何实现这一目标?我查看了很多示例,但确实在堆栈溢出方面找到了类似的示例。

这是我的意见

let dateStrings = [
"2016-12-20 16:00:00", 
"2016-12-20 16:15:00", 
"2016-12-21 16:00:00", 
"2016-12-22 16:00:00", 
"2016-12-22 16:15:00", 
"2016-12-22 16:30:00"
]

我想要的输出

let categorisedAppointmentTimes = [ 
["Tue, 20 Dec" : ["4:00 pm", "4:15 pm"]],
["Wed, 21 Dec" : ["4:00 pm"]],
["Thu, 22 Dec" : ["4:00 pm", "4:15 pm", "4:30 pm"]]
]

【问题讨论】:

    标签: ios arrays swift


    【解决方案1】:

    您需要使用DateFormatterString 解析为Date 并从Date 解析为String

    也请阅读本文以进一步了解这些运算符。 http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/

        var categorisedAppointmentTimes : [String : [String]] = [:]
    
        let dateStrings = [
            "2016-12-20 16:00:00",
            "2016-12-20 16:15:00",
            "2016-12-21 16:00:00",
            "2016-12-22 16:00:00", 
            "2016-12-22 16:15:00", 
            "2016-12-22 16:30:00"
        ]
    
    
        let inputDateFormatter = DateFormatter()
        inputDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    
        let keyDateFormatter = DateFormatter()
        keyDateFormatter.dateFormat = "EEE, dd MMM"
    
        let valueDateFormatter = DateFormatter()
        valueDateFormatter.dateFormat = "h:mm a"
    
    
        for dateString in dateStrings {
            let date = inputDateFormatter.date(from: dateString)
    
            let dateKey = keyDateFormatter.string(from: date!)
    
            let dateValue = valueDateFormatter.string(from: date!)
    
            if categorisedAppointmentTimes.index(forKey: dateKey) != nil {
                categorisedAppointmentTimes[dateKey]!.append(dateValue)
            }else{
                categorisedAppointmentTimes[dateKey] = [dateValue]
            }
        }
    
        print(categorisedAppointmentTimes)
    
        //["Tue, 20 Dec": ["4:00 PM", "4:15 PM"],
        // "Thu, 22 Dec": ["4:00 PM", "4:15 PM", "4:30 PM"],
        // "Wed, 21 Dec": ["4:00 PM"]]
    

    【讨论】:

    • 谢谢,我注意到这些天不是按升序排列的,我需要它们按升序排列,即 20、21 和 22 日。
    • 是的,如果你愿意,你需要排序,然后你不能使用字典(或者至少不是在这个结构中,你可能需要添加一个字段来存储)来存储秒 'timeIntervalSince1970 ')。字典是“无序的集合,你通过键访问字典值,所以顺序无关紧要。*.com/questions/31088973/…
    【解决方案2】:

    我认为这将实现您想要的,但它非常特定于这种情况,您必须确保 dateStrings 始终采用您共享的格式。 “日期时间”

     var categorisedAppointmentTimes : [String : [String]] = [:]
        let dateStrings = [
            "2016-12-20 16:00:00",
            "2016-12-20 16:15:00",
            "2016-12-21 16:00:00",
            "2016-12-22 16:00:00",
            "2016-12-22 16:15:00",
            "2016-12-22 16:30:00"
        ]
        dateStrings.sorted { (s1, s2) -> Bool in
            s1 > s2
        }
        for date in dateStrings {
            let stringComponets = date.components(separatedBy: " ")
            guard let _ = categorisedAppointmentTimes[stringComponets[0]] else {
                categorisedAppointmentTimes[stringComponets[0]]  = [stringComponets[1]]
                continue
            }
            categorisedAppointmentTimes[stringComponets[0]]?.append(contentsOf: [stringComponets[1]])
    
        }
    

    【讨论】: