比较两个 NSDate 的相同日期/时间

发布于 2024-11-01 03:58:23 字数 658 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂ガ小子 2024-11-08 03:58:23

您正在比较两个指针值。您需要使用 NSDate 比较方法,例如:

return ([date1 compare:date2] == NSOrderedSame);

You're comparing two pointer values. You need to use the NSDate comparison method like:

return ([date1 compare:date2] == NSOrderedSame);
爱殇璃 2024-11-08 03:58:23

与 C 语言(Objective-C 是其超集)一样,==(相等)运算符比较两个指针值以查看它们是否相等(即两个变量是否持有相同的对象) 。虽然这适用于比较原始值(intcharbool),但它不适用于 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 (ints, chars, bools), 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 compare NSDates, or -isEqualToString: used to compare NSStrings. These methods cannot be used to compare primitive types (ints, for instance) because those are not objects, but will work on almost all objects.

悸初 2024-11-08 03:58:23

你不能在 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文