更新: これは実際の例です。
まず、曜日、時間、分、秒を保持するクラスを作成します。
myClass.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface myClass : NSObject {
NSString *weekday;
NSInteger hour;
NSInteger minute;
NSInteger second;
}
@property (nonatomic, strong) NSString *weekday;
@property (nonatomic, assign) NSInteger hour;
@property (nonatomic, assign) NSInteger minute;
@property (nonatomic, assign) NSInteger second;
@end
myClass.m
#import "myClass.h"
@implementation myClass
@synthesize weekday, hour, minute, second;
@end
次に、日付情報を保持する myClass のインスタンスを作成する必要があります。
これを ViewController.h に追加します。
@property (nonatomic, strong) NSMutableArray *myArray;
このコードは、ViewController.m の好きな場所に配置します。
myArray = [[NSMutableArray alloc] init];
//Setup an instance of myClass
myClass *c = [[myClass alloc] init];
[c setWeekday:@"Monday"];
[c setHour:13];
[c setMinute:0];
[c setSecond:0];
[myArray addObject:c];
次に、私たちのイベントがどれだけ未来にあるのかを把握する必要があります。rdelmar のおかげで、これを行うためのコードが得られました。彼の答えは以下のとおりです。
//Create a func that returns an NSDate. It requires that the Weekday, HR, Min and Secs are passed into it.
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
//Setup an array of weekdays to compare to the imported (NSString *)weekday
NSArray *array = [NSArray arrayWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
//This code finds how many days in the future the imported (NSString *)weekday is
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
//Lastly, create an NSDate called eventDate that consists of the
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
return eventDate;
}
ここでは、新しく作成された eventDate を取得し、それを使用して iCal でイベントを作成します。
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Move your car!";
event.startDate = eventDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:60.0f * 60.0f sinceDate:event.startDate]; //1 hr long
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -30.0f]]; //30 min before
//eventLoc was created using CLGeocoder and the method reverseGeocodeLocation:
//The location is not necessary to create an event but if you'd like the code, ask and i'll post it.
[event setLocation:eventLoc];
[event setNotes:@"This event was set by me. ;P"];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"Event Set");
これが私を助けたのと同じくらい誰かに役立つことを願っています。
:更新の終わり:
「次の月曜日の午後 1 時」を簡単に見つける方法を見つけたいと思って、NSDate のドキュメントを読みました。
たとえば、ベーカリーが週に 1 日 (木) 午前 9 時から午後 6 時まで営業しているとしましょう。今日が木曜日の午後 7 時である場合、次の木曜日の午前 9 時に NSDate が必要です。
iCal でイベントを作成する予定ですが (テストは成功しています)、問題はイベント時間の計算です。
NSDate の適切な説明を教えてください。または、探している NSDate の計算方法を理解するのを手伝ってもらえますか?
私はこのコードを修正しようとしています:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Bakery's Open!";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];