Xcode 開発で「今日は、今年何日目か」を実装する方法

Xcode 開発で、かつて作成したアプリのカレンダ―関連機能で「 iOS 8.0 では最初は廃止予定」のメッセージが発生したので最新環境に対応した調査を行いました。

エラーメッセージは、警告レベル?なのでこのままでも問題ないようですが…

ーーーエラーメッセージーー
‘NSWeekOfYearCalendarUnit’ is deprecated: first deprecated in iOS 8.0 – Use NSCalendarUnitWeekOfYear instead

ーーー日本語訳ーーー
‘NSWeekOfYearCalendarUnit ‘は廃止されました:iOS 8.0では最初は廃止予定 – 代わりにNSCalendarUnitWeekOfYearを使用


今日は、今年何日目かを取得する機能を Xcode で実装してみました。Objective-c仕様

ポイント
・算出に当たって、の月・の月の判断は面倒
・できるだけObjective-c関数で対応

手順
今日は、何月何日何曜日かを取得
元日は、何曜日かを取得
先週までの日数を計算 + 今日は今週何日目 - 元日の曜日より1日マイナス


例 2018年7月1日の場合

・今日は、7月1日日曜日(1)を取得、今週は、第27週目
・元日は、月曜日(2)を取得
(先週は、第何週)* 7日 + 今週日曜日から何日目 - 元日は、日曜日から何日目 - 元日分マイナス
(27 – 1) * 7 + 1 - (2 – 1)  = 182

・古典的な方法では、経過日で算出するので1月~6月まで+1日分の計算
31 + 28 + 31 + 30 + 31 + 30 + 1 = 182

1   NSDate *today = [NSDate date];
2     
3    // NSDateComponentsで年・月・日・曜日を取得する
4    NSCalendar *calendar = [NSCalendar currentCalendar];
5     
6    NSDateComponents *components =
7[calendar components: NSCalendarUnitYear
8| NSCalendarUnitMonth|NSCalendarUnitDay |
9NSCalendarUnitWeekday NSCalendarUnitHour|
10NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:today];
11     
12    //今時点の 年 月 日 曜日 (時 分 秒)
13    NSInteger Wyear = components.year;
14    NSInteger Wmonth = components.month;
15    NSInteger Wday = components.day;
16    
17    NSInteger Wweekday = components.weekday;
18//1:日曜日 2:月曜日 3:火曜日 4:水曜日 5:木曜日 6:金曜日 7:土曜日
19     
20    NSInteger Whour = components.hour;//今回の処理に無意味です。
21    NSInteger Wmin = components.minute;//今回の処理に無意味です。
22    NSInteger Wsec = components.second;//今回の処理に無意味です。
23     
24    //今日は今年第何週目
25    NSCalendar* calendar2 = [NSCalendar currentCalendar];
26    NSDateComponents* components2 =
27[calendar2 components:NSCalendarUnitWeekOfYear fromDate:today];
28    NSInteger Wthisweek = components2.weekOfYear;
29     
30    //今年の開始(元日)は何曜日
31    NSCalendar *calendar3 = [NSCalendar currentCalendar];
32     
33    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
34    [fmt setDateFormat:@"yyyyMMdd"];
35     
36     NSString *date0101 = @"20180101";//2018年固定の場合
37     
38    NSDate *formatterdate0101 = [fmt dateFromString:date0101];
39     
40    NSDateComponents *components3 =
41[calendar3 components: NSCalendarUnitYear
42| NSCalendarUnitMonth| NSCalendarUnitDay
43 | NSCalendarUnitWeekday
44fromDate:formatterdate0101];
45     
46    NSInteger weekday0101 = components3.weekday;//元日の曜日取得
47     
48    //先週までの日数 + 今週日曜からの日数分 - 元日週の元日までの日数
49    NSInteger Count_day = (Wthisweek - 1) * 7
50+ Wweekday - (weekday0101 - 1) ;
51//先週まで × 7日 + 今週分 - 今年の0101日は、週のいつに始まったか
52    

古典的なロジックでは、以下の方法が容易と思われます。ポイントは、caseにbreakを入れていない点です。上記の「今日は今年第何週目」以降の差し替えになります。

1NSInteger Count_day = 0;
2 
3switch (Wmonth - 1) { //前月までの加算
4    case 11:
5        Count_day = Count_day + 30;
6    case 10:
7        Count_day = Count_day + 31;
8    case 9:
9        Count_day = Count_day + 30;
10    case 8:
11        Count_day = Count_day + 31;
12    case 7:
13        Count_day = Count_day + 31;
14    case 6:
15        Count_day = Count_day + 30;
16    case 5:
17        Count_day = Count_day + 31;
18    case 4:
19        Count_day = Count_day + 30;
20    case 3:
21        Count_day = Count_day + 31;
22    case 2:
23        Count_day = Count_day + 28;
24         
25        if ((Wyear % 4) == 0){
26             
27            Count_day = Count_day + 1;
28        }
29         
30    case 1:
31        Count_day = Count_day + 31;
32     
33    case 0:
34        Count_day = Count_day + Wday;//当月の日数
35     
36         
37         
38    default:
39        break;
40}

 

Verified by MonsterInsights