在Swift中迭代日期范围最有效的方法是什么? [英] What is the most effective way to iterate over a date range in Swift?

查看:83
本文介绍了在Swift中迭代日期范围最有效的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Swift习惯建立应用程序.在某个时候,用户可以选择他们想要建立的习惯的持续时间(以天为单位).用户还可以选择一周中的哪几天进行练习(周日,周一,周二等).开始日期是他们养成习惯的日子.

I am building a Swift habit-building application. At a certain point, a user selects the duration (in days) of the habit they'd like to create. The user can also select which days of the week they'd like to practice it (sunday, monday, tuesday, etc.). The start date is the day they create the habit.

我目前有开始日期和结束日期(基于用户选择的天数).但是,在该日期范围内,我需要提取用户选择的特定日期的日期.

I currently have the start date and end date (based on number of days selected by user). Within that date range, however, I need to extract dates for the specific days the user has chosen.

我的想法是,对于时间间隔(startDate,endDate)中的每个工作日值"x",返回日历日期.但是,我尝试创建一个DateInterval,但由于它不符合协议序列",所以无法对其进行迭代.

My thinking is that for every weekday value 'x' within time interval (startDate, endDate), return Calendar date. However, I have tried creating a DateInterval, and I am unable to iterate over it because it does not conform to protocol 'Sequence'.

是否有一种有效的方法可以在此日期范围内进行迭代并提取其他日期?还有我没有想到的更好的方法吗?

Is there an efficient way of iterating over this date range and extracting these other dates? Is there a better way that I'm not yet thinking of?

以下是我当前的日期入门代码:

Here's my current date starter code below:

var date = Date()
var duration = 10
let calendar = Calendar.current

let dateEnding = Calendar.current.date(byAdding: .day, value: duration, to: date as Date)!

//Returns a number representing day of the week.  i.e. 1 = Sunday
let weekDay = calendar.component(.weekday, from: date)


//Initializing a time interval, based on user selected duration and start date
let timeInterval = DateInterval.init(start: date, end: dateEnding)

感谢您的帮助...

推荐答案

如果我正确理解了您的问题,则用户将在某些工作日进行核对并提供持续天数.

If I understand your question correctly, the user will check off some weekdays and provide a duration as a number of days.

假设您在数组中选择了工作日和持续时间,则可以获取匹配日期的列表,如下所示:

Assuming you have the selected weekdays in an array and the duration, you can get the list of matching dates as follows:

// User selected weekdays (1 = Sunday, 7 = Saturday)
var selectedWeekdays = [2, 4, 6] // Example - Mon, Wed, Fri
var duration = 10 // Example - 10 days

let calendar = Calendar.current
var today = Date()
let dateEnding = calendar.date(byAdding: .day, value: duration, to: today)!

var matchingDates = [Date]()
// Finding matching dates at midnight - adjust as needed
let components = DateComponents(hour: 0, minute: 0, second: 0) // midnight
calendar.enumerateDates(startingAfter: today, matching: components, matchingPolicy: .nextTime) { (date, strict, stop) in
    if let date = date {
        if date <= dateEnding {
            let weekDay = calendar.component(.weekday, from: date)
            print(date, weekDay)
            if selectedWeekdays.contains(weekDay) {
                matchingDates.append(date)
            }
        } else {
            stop = true
        }
    }
}

print("Matching dates = \(matchingDates)")

这篇关于在Swift中迭代日期范围最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆