-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMeetupExample.m
64 lines (54 loc) · 1.62 KB
/
MeetupExample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#import "MeetupExample.h"
@implementation Meetup {
int _year;
int _month;
}
- (instancetype)initWithYear:(int)year andMonth:(int)month {
if (self = [super init]) {
_year = year;
_month = month;
}
return self;
}
- (NSDate *)dayForDayOfWeek:(MeetupDayOfWeek)dayOfWeek andOptions:(MeetupOptions)options {
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = _year;
components.month = _month;
switch (options) {
case MeetupOptionsFirst:
components.day = 1;
break;
case MeetupOptionsSecond:
components.day = 8;
break;
case MeetupOptionsThird:
components.day = 15;
break;
case MeetupOptionsFourth:
components.day = 22;
break;
case MeetupOptionsLast:
components.day = 0;
components.month += 1;
break;
case MeetupOptionsTeenth:
components.day = 13;
break;
default:
break;
}
while ([self weekdayFromComponents:components] != dayOfWeek) {
if (options == MeetupOptionsLast) {
components.day -= 1;
} else {
components.day += 1;
}
}
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
- (long)weekdayFromComponents:(NSDateComponents *)components {
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
long weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:date];
return weekday - 1;
}
@end