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:
- NSDateFormatter and +00:00 parsing
- Fast and elegant delegation in Objective-C