Objective C != Operator overloading

12 Oct
2009

Yeah I know… ObjC does not overload operators… but I’m so used to this feature, that this is a source if mistakes for me…

Always remember to use compare, isEqual or else!

Here is a snipped to compare dates

#import
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDate *dateOne = [NSDate date];
sleep(3);
NSDate *dateTwo = [NSDate date];
switch ([dateOne compare:dateTwo]) {
case NSOrderedSame:
NSLog(@"These dates are the same!");
break;
case NSOrderedAscending:
NSLog(@"dateOne is earlier than dateTwo!");
break;
case NSOrderedDescending:
NSLog(@"dateTwo is earlier than dateOne!");
break;
default:
NSLog(@"Bad times. Invalid enum value returned.");
break;
}
 [pool release];
return 0;
}

Related posts:

  1. NSDateFormatter and +00:00 parsing
  2. Objective C Blocks with Leopard 10.5 and iPhone 3.0
  3. Detect conflicting Objective C category methods

Comment Form

top

Switch to our mobile site