开发者

If statement being temperamental

开发者 https://www.devze.com 2023-03-13 17:28 出处:网络
I\'m not sure but sometimes my if statement fails and show the wrong thing when it shouldn\'t be Any ideas?

I'm not sure but sometimes my if statement fails and show the wrong thing when it shouldn't be Any ideas?

-(void)updateLabel{



   if (today > Date) {



    [only setHidden:YES];
    [untilRelease setHidden:YES];
    [theLabel setText:@"IS OUT"];


} else {


    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *components = [calendar components:units from开发者_如何学PythonDate:[NSDate date] toDate:Date options:0];

    [theLabel setText:[NSString stringWithFormat:@"%d %@, %d %@ \n %d %@, %d %@ %d %@", [components month], @"Months", [components day], @"Days", [components hour], @"Hours", [components minute], @"Minutes and", [components second], @"Seconds"]];


}}

I have defined today as [NSDate date] and Date is an epoch number.

Any help would be great


Since they are both NSDate instances, do this,

NSComparisonResult result = [today compare:Date];
if ( result == NSOrderedDescending ){ //  today > Date
    // After Date
} else {
    // Before Date
}

Original Answer

If today is [NSDate date] and Date is an epoch number, you are comparing them incorrectly. You should convert today to epoch number too prior to comparing them.

long currentTime = [today timeIntervalSince1970];
if ( currentTime > Date ) {
    // After `Date`
} else {
    // Before `Date`
}
0

精彩评论

暂无评论...
验证码 换一张
取 消