我有以下课程:
#import "Period.h"
@implementation Period
...
- (BOOL)isEqualTo:(id)object {
return [self isEqual:object];
}
- (BOOL)isEqual:(id)object {
if (object == self) {
return YES;
}
if ([[object beginDate] hash] == [[self beginDate] hash] &&
[[object endDate] hash] == [[self endDate] hash]) {
return YES;
}
return NO;
}
...
@end
还有以下测试,使用OCUnit
:
Period *period;
NSDate *beginDate;
NSDate *endDate;
- (void)setUp {
beginDate = [NSDate dateWithString:@"2011-02-25"];
endDate = [NSDate dateWithString:@"2011-03-25"];
period = [[Period alloc] initWithBeginDate:beginDate
endDate:endDate];
}
- (void)testEndDateShouldBeGreaterOrEqualThanBeginDate {
Period *newPeriod = [[Period alloc] initWithBeginDate:beginDate
endDate:beginDate];
STAssertEqualObjects(beginDate, [newPeriod beginDate], @"Begin dates are different");
STAssertEqualObjects(endDate, [newPeriod endDate], @"End dates are different");
}
以前我使用STAssertEquals
而不是,STAssertEqualObjects
它没有isEqual
在Period
.
我只想明白两点:
- 这两种方法有什么区别?
isEqual
和有什么区别isEqualTo
?