【问题标题】:Xcode: Do something on specific dateXcode:在特定日期做某事
【发布时间】:2013-03-26 09:04:25
【问题描述】:

您好,我正在尝试在特定日期做某事,目前我只是记录一些随机的东西,但日志会在应用程序启动时直接出现,而不是在我设置的日期出现。这是我的代码。

-(void)theMagicDate{

    NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

    [nextComp setYear:2013];
    [nextComp setMonth:3];
    [nextComp setDay:26];
    [nextComp setHour:10];
    [nextComp setMinute:05];

    UIDatePicker *nextDay = [[UIDatePicker alloc]init];
    [nextDay setDate:[nextCal dateFromComponents:nextComp]];

    if(nextDay.date){
        NSLog(@"Doing the stuff on the date");
    }
}

我从 viewDidLoad 调用这个函数

【问题讨论】:

    标签: objective-c xcode date nsdate


    【解决方案1】:

    嗯,你做了一些错误的假设:

    第一个if(nextDay.date){ 将始终为真。因为它只会检查是否有任何东西分配给财产日期。由于您为该属性分配了日期,因此它将是真实的。

    其次,UIDatePicker 是一个用户界面 (UI) 组件,允许用户选择日期。 如果您想检查您使用组件创建的日期是否已粘贴、现在或将来,您必须使用NSDate 上的方法。

    像这样:

    -(void)theMagicDate{
    
    NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
    
    [nextComp setYear:2013];
    [nextComp setMonth:3];
    [nextComp setDay:26];
    [nextComp setHour:10];
    [nextComp setMinute:05];
    
    NSDate *dateToCheck = [nextCal dateFromComponents:nextComp];
    NSDate *now = [NSDate date];
    
    
    switch ([now compare:dateToCheck]) {
        case NSOrderedAscending:
            NSLog(@"Date in the future");
            break;
    
        case NSOrderedDescending:
            NSLog(@"Date in the past");
            break;
    
        case NSOrderedSame:
            NSLog(@"Date is now");
            break;
    }
    
    
    }
    

    【讨论】:

    • 我正在尝试这种方法,但即使我试图将当前日期和时间设置为未来 1 分钟,我也只能让字符串 @"Date in the future" 工作,但它没有不知道那个日期是什么时候过去的。
    • 如果您在检查日期之前将日期设置为未来一分钟,那么它仍然在未来。如果我运行上面的例子,它会给出一个控制台日志:Date in the past
    • 好的,所以我让它工作了,但只有在重启手机时。我想要的是这个视图应该是活跃的和运行的,并且仍然启动将在特定日期和时间发生的事情。
    【解决方案2】:
    if(nextDay.date){
        NSLog(@"Doing the stuff on the date");
    }
    

    总是正确的。

    您需要将当前日期与您从选择器或任何地方选择的日期进行比较。您需要将未来日期保存在 userdefaults 或 plist 等中。在 MagicDate 方法中读取并比较两个日期,然后移动到NSLog(@"Doing the stuff on the date");

    -(void)theMagicDate{
    
        NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
    
        [nextComp setYear:2013];
        [nextComp setMonth:3];
        [nextComp setDay:26];
        [nextComp setHour:10];
        [nextComp setMinute:05];
    
        UIDatePicker *nextDay = [[UIDatePicker alloc]init];
        [nextDay setDate:[nextCal dateFromComponents:nextComp]];
    
    
         //read from plist etc
         NSDate *readDate=...
    
    
        if( [readDate compare:nextDay.date]==NSOrderedSame){
            NSLog(@"Doing the stuff on the date");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-22
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多