我有一个 NSDate
必须与其他两个 NSDate
进行比较,我尝试使用 NSOrderAscending
和 NSOrderDescending
但是如果我的日期与其他两个日期相同?
示例:如果我有一个 myDate = 24/05/2011
和另外两个是一个 = 24/05/2011
和两个 24/05/2011
我可以用什么?
最佳答案
根据Apple documentation of NSDate compare:
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
- (NSComparisonResult)compare:(NSDate *)anotherDate
Parameters
anotherDate
The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.
Return Value
If:
The receiver and anotherDate are exactly equal to each other,
NSOrderedSame
The receiver is later in time than anotherDate,
NSOrderedDescending
The receiver is earlier in time than anotherDate,
NSOrderedAscending
换句话说:
if ([date1 compare:date2] == NSOrderedSame) ...
请注意,在您的特定情况下,阅读和编写此内容可能更容易:
if ([date2 isEqualToDate:date2]) ...
见 Apple Documentation about this one .
https://stackoverflow.com/questions/6112075/