如果陈述有脾气
我不确定,但有时我的 if 语句会失败并在不应该的情况下显示错误的内容 有什么想法吗?
-(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 fromDate:[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"]];
}}
我将今天定义为 [NSDate date],Date 是纪元号。
任何帮助都会很棒
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 fromDate:[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于它们都是
NSDate
实例,因此执行此操作,原始答案
如果
today
是[NSDate date]
且Date
是纪元数,您对它们的比较不正确。在比较它们之前,您也应该将今天
转换为纪元号。Since they are both
NSDate
instances, do this,Original Answer
If
today
is[NSDate date]
andDate
is an epoch number, you are comparing them incorrectly. You should converttoday
to epoch number too prior to comparing them.