0

我有一个学校应用程序可以跟踪您的日程安排。该应用程序有多种输入时间表的选项。如果用户有四天的时间表,那么他们将有 AD 天。对于我的应用程序。我想跟踪今天是什么日子(例如,它是 A 日还是 C 日)。用户在设置应用程序时将能够指定现在是哪一天,但应用程序应每天跟踪此更改,不包括周末。我不确定实现这一点的最佳方法是什么。

4

2 回答 2

0

如果配置允许完全任意的日期分配,您只需要 [Date:String] 形式的简单字典。

如果您想让您的用户指定日期标识符(字母)的重复模式,则配置参数和过程将需要更加复杂,并且取决于您愿意在处理异常(即非上学日)方面走多远。周末可以是自动的,但假期和预定的“休息日”需要您定义某种规则(硬编码或用户可配置)。

在所有情况下,我建议创建一个函数,根据配置的参数将任何日期转换为其日期标识符。根据规则的复杂性和您正在使用的日期范围,动态构建日期标识符的全局字典(例如 [Date:String])并且每个日期只计算一次标识符可能是一个好主意。

例如:

// your configuration parameters could be something like this:

let firstSchoolDay:Date     = // read from configuration
let lastSchoolDay           = // read from configuration
let dayIdentifiers:[String] = // read from configuration 
let skippedDates:Set<Date>  = // read from configuration 

// The global dictionary and function could work like this:

var dayIdentifiers:[Date:Sting] = [:] // global scope (or singleton)
func dayIdentifier(for date:Date) -> String
{
    if let dayID = dayIdentifiers[date]
    { return dayID  }

    let dayID:String = // compute your day ID once according to parameters.
                       // you could use an empty string as a convention for
                       // non school days

                       // a simple way to compute this is to start from
                       // the last computed date (or firstSchoolDay)
                       // and move forward using Calendar.enumerateDates
                       // applying the weekend and offdays rules
                       // and saving day identifiers as you
                       // move forward up to the requested date

    dayIdentifiers[date] = dayID
    return dayID         
}
于 2017-06-11T00:16:16.760 回答
0

我想出了如何跟踪移动日期,同时也不考虑周末并在每次检查时更新值。

func getCurrentDay() -> Int {

    let lastSetDay = //Get the last value of the set day
    let lastSetDayDate = //Get the last time the day was set
    var numberOfDays: Int! //Calculate number of recurring days, for instance an A, B, C, D day schedule would be 4

    var currentDay = lastSetDay
    var currentIteratedDate = lastSetDayDate

    while Calendar.current.isDate(currentIteratedDate, inSameDayAs: Date()) == false {
        currentIteratedDate = Calendar.current.date(byAdding: .day, value: 1, to: currentIteratedDate)!
        if !Calendar.current.isDateInWeekend(currentIteratedDate) {
            currentDay += 1
        }
        if currentDay > numberOfDays {
            currentDay = 1
        }
    }

    if Calendar.current.isDate(currentIteratedDate, inSameDayAs: Date()) == false {
        //Save new day
        //Save new date
    }

    return currentDay
}
于 2017-06-11T18:24:53.987 回答