目标c – NSDate的NSNumber

我试图绕过一个拒绝早于明天采取任何事情的日期验证.
到目前为止我有这个:

NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
                // Negative one day, in seconds (-60*60*24)
NSLog(@"%@", [NSDate date]);
    // "yyyy-MM-dd HH:mm:ss Z", accurate assuming Z = +0000
NSLog(@"%@", dateY);
    // "yyyy-MM-dd HH:mm:ss Z", same accuracy (minus one day)

那太好了,但dateY不是NSNumber.我需要一个NSNumber进行比较,但我找不到任何有用的东西. (我甚至不知道一个NSNumber可以是2011-04-14 13:22:29 0000,反正…)

我可以使用NSDateFormatter将NSDate转换为NSString,因此如果可以使用该字符串并将其转换为所需的NSNumber(而不是将NSDate直接转换为NSNumber,我似乎无法找到帮助)与任何一个),这将是罚款.

- (BOOL)validateDueDate:(id *)ioValue error:(NSError **)outError {
    NSDate *dateY = [NSDate dateWithTimeIntervalSinceNow:-86400];
    NSNumber *tis1970 = [NSNumber numberWithDouble:[dateY timeIntervalSince1970]];
    NSLog(@"NSNumber From Date : %@", tis1970);
    NSLog(@"Date From NSNumber : %@", [NSDate dateWithTimeIntervalSince1970:[tis1970 doubleValue]]);

    // Due dates in the past are not valid
    // Enforced that a due date has to be >= today's date
    if ([*ioValue compare:[NSDate date]] == NSOrderedAscending) {
        if (outError != NULL) {
            NSString *errorStr = [[[NSString alloc] initWithString:@"Due date must be today or later."] autorelease];
            NSDictionary *userInfoDictionary = [NSDictionary dictionaryWithObject:errorStr forKey:@"ErrorString"];
            NSError *error = [[[NSError alloc]
                                initWithDomain:TASKS_ERROR_DOMAIN
                                code:DUEDATE_VALIDATION_ERROR_CODE
                                userInfo:userInfoDictionary] autorelease];
            *outError = error;
            }
        return NO;
    } else {
        return YES;
    }
}

现在,用户不能在明天之前选择一个日期. errorStr谎言在今天之前比以前更明白,作为拒绝拯救日期的一个原则,所以我一直在和这件事作斗争,让我昨天替代今天,而不是深入.

编辑:使用NSOrderedSame允许选择任何日期而不会出错.那不行.

您可以将NSDate转换为NSNumber,如下所示:

NSDate *aDate = [NSDate date];
NSNumber *secondsSinceRefDate = [NSNumber numberWithDouble:[aDate timeIntervalSinceReferenceDate]];

并转换回如下:

aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:[NSNumber doubleValue]];
翻译自:https://stackoverflow.com/questions/5664276/nsnumber-from-nsdate

转载注明原文:目标c – NSDate的NSNumber