两个 NSDate 的问题
我对这段代码有疑问:
NSDate *today = [[NSDate alloc] init];
if (dateOne == today) {
dataLabel.textColor = [UIColor redColor]; NSLog(@"inside if");}
NSLog(@"today:%@", oggi);
NSLog(@"dateOne:%@", dateOne);
控制台的结果是
2011-06-10 17:19:00.170 Project[678:707] today:2011-06-10 15:19:00 +0000
2011-06-10 17:19:00.174 Project[678:707] dateOne:2011-06-10 15:19:00 +0000
我也尝试“isEqualToDate”,但代码没有输入“if”内
I have a problem with this code:
NSDate *today = [[NSDate alloc] init];
if (dateOne == today) {
dataLabel.textColor = [UIColor redColor]; NSLog(@"inside if");}
NSLog(@"today:%@", oggi);
NSLog(@"dateOne:%@", dateOne);
the result of console is
2011-06-10 17:19:00.170 Project[678:707] today:2011-06-10 15:19:00 +0000
2011-06-10 17:19:00.174 Project[678:707] dateOne:2011-06-10 15:19:00 +0000
I also try "isEqualToDate" but the code don't entry inside "if"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我了解您正在尝试确定给定的
NSDate
是否是今天。为此,您无需在比较中考虑小时、分钟等。怎么样:方法
isEqualToDate:
不会执行这个工作,因为它检查给定日期是否与接收者完全相等(具有亚秒精度)。==
运算符也不适合这种情况,因为它将指针与对象进行比较,它检查指针是否指向同一内存位置。I understand you are trying to determine if a given
NSDate
is today. To do this you don't have to take in consideration hours, minutes, etc. in your comparison. How about this:The method
isEqualToDate:
won't do this job because it checks whether a given date is exactly equal the receiver (with sub-second precision).The
==
operator is not appropriate for this either because it compares the pointers to the objects, it checks whether the pointers point to the same memory location.==
比较指针以查看它们是否指向同一个对象,而不是对象本身的值。要比较日期,请使用-[NSDate isEqualToDate:]
:==
compares pointers to see if they point to the same object -- not the values of the object themselves. To compare dates, use-[NSDate isEqualToDate:]
: