0

更新:这是工作示例。

首先,我们创建一个类来保存工作日、小时、分钟和秒:

我的类.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

我的班级.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");

我希望这对某人有帮助,就像它对我有帮助一样。

:更新结束:

我已经阅读了 NSDate 的文档,希望找到一种简单的方法来找到“下一个即将到来的周一下午 1:00”。

例如,假设面包店每周 1 天(星期四)从上午 9 点到下午 6 点营业……如果现在是星期四上午 8 点,我想从现在开始获得 1 小时的 NSDate。如果今天是星期四 @ 晚上 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]; 
4

1 回答 1

1

要找到这样的日期需要使用 NSDateComponents 和 NSCalendar。我认为,主要任务是弄清楚你的目标日期在未来多少天。为此,您需要获取当前日期的工作日日期部分,并计算距您要查找的工作日还有多少天。我认为下面的代码应该让你接近:

-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
NSArray *array = [NSArray arrayWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
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;

NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
NSDate *eventDate = [cal dateByAddingComponents:eventComps toDate:now  options:0];
[eventComps release];
return eventDate;

}

于 2012-03-23T02:56:00.017 回答