比较两个 NSDate 的相同日期/时间
date1 == date2
不是有效的比较方法吗?如果不是,正确的选择是什么?
这是我的代码:
- (NSDate*) dateWithNoTime {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:self];
NSDate* dateOnly = [calendar dateFromComponents:components];
return dateOnly;
}
- (BOOL) sameDayAsDate:(NSDate*)dateToCompare {
NSDate *date1 = [self dateWithNoTime];
NSDate *date2 = [dateToCompare dateWithNoTime];
return date1 == date2; // HERE IS WHERE THINGS SEEM TO FAIL
}
Is date1 == date2
not a valid way to compare? If not, what is the correct alternative?
Here's my code:
- (NSDate*) dateWithNoTime {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:self];
NSDate* dateOnly = [calendar dateFromComponents:components];
return dateOnly;
}
- (BOOL) sameDayAsDate:(NSDate*)dateToCompare {
NSDate *date1 = [self dateWithNoTime];
NSDate *date2 = [dateToCompare dateWithNoTime];
return date1 == date2; // HERE IS WHERE THINGS SEEM TO FAIL
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在比较两个指针值。您需要使用 NSDate 比较方法,例如:
You're comparing two pointer values. You need to use the NSDate comparison method like:
与 C 语言(Objective-C 是其超集)一样,
==
(相等)运算符比较两个指针值以查看它们是否相等(即两个变量是否持有相同的对象) 。虽然这适用于比较原始值(int
、char
、bool
),但它不适用于 Objective-C 对象,因为内容可能相同,但内存位置不同(这是相等运算符比较的内容)。要检查两个对象是否相等,
NSObject
提供了-isEqual:
方法,您可以将其用作一般语句(例如[date1 isEqual:date2]),有些类选择提供更具体的比较方法,例如用于比较
NSDate
的-isEqualToDate:
或用于比较-isEqualToString:
code> 用于比较NSString
。这些方法不能用于比较基本类型(例如 int),因为它们不是对象,但适用于几乎所有对象。As in the C language (of which Objective-C is a superset), the
==
(equality) operator compares two pointer values to see if they are equivalent (i.e. if two variables hold the same object). While this works in comparing primitive values (int
s,char
s,bool
s), it does not work on Objective-C objects, which might be equal in content, but differ in memory location (which is what the equality operator compares).To check if two objects are equal,
NSObject
offers an-isEqual:
method which you can use as a general statement (e.g.[date1 isEqual:date2]
), and some classes choose to offer a more specific comparison method, such as-isEqualToDate:
used to compareNSDate
s, or-isEqualToString:
used to compareNSString
s. These methods cannot be used to compare primitive types (int
s, for instance) because those are not objects, but will work on almost all objects.你不能在 Objective-C 中使用
==
来比较对象相等性(它将采用 C 的含义,比较指针)。与其他语言一样,您只是比较对象指针。您想要的消息是
isEqualToDate:
,又名[date1 isEqualToDate:date2]
You can't use
==
in Objective-C to compare object equality (it will take the C meaning, comparing pointers). Like other languages, you are simply comparing the object pointers.The message you want is
isEqualToDate:
, aka[date1 isEqualToDate:date2]