2017-06-10 2 views
0

私はあなたのスケジュールを追跡する学校のアプリを持っています。アプリは、スケジュールを入力するための多数のオプションがあります。ユーザーが4日間のスケジュールを持っている場合、彼らはA-D日を持つでしょう。私のアプリのため。私は現在の日が何であるかを把握したいと思います(例えば、A dayかC dayかなど)。ユーザーはアプリを設定する日を指定できるようになりますが、アプリは日々の変化から週末を引いた日の変化を把握する必要があります。私はこれを実装する最良の方法は何か分かりません。日付のあるデータの移動を追跡するにはどうすればよいですか?

答えて

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 
} 
0

完全に任意の曜日の割り当てが許可されている場合は、[日付:文字列]の形式の単純な辞書のみが必要です。

ユーザに曜日識別子(文字)の繰り返しパターンを指定させる場合、設定パラメータとプロセスはより洗練されたものでなければならず、例外処理にどれくらいの距離(すなわち、学生時代)。週末は自動でもかまいませんが、休日や予定された「休み日」には、何らかのルール(ハードコードまたはユーザー設定可能)を定義する必要があります。

すべての場合、設定されたパラメータに基づいて日付を日の識別子に変換する関数を作成することをお勧めします。ルールの複雑さと使用する日付の範囲によっては、日付識別子([日付:文字列]など)のグローバル辞書を動的に作成し、日付ごとに識別子を1回だけ計算することをお勧めします。

例えば:

// 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   
} 
関連する問題