Reputation: 20378
I am making some changes to an existing library which uses the "addTimeInterval:" function which was deprecated in OS X v10.6. I would like to supress the warning using a preprocessor directive to check which SDK version the current build is using. Something like this:
NSDate *newDate= nil;
#if OS X v10.6 (or newer)
newDate= [someOtherDate dateByAddingTimeInterval:60];
#else
newDate= [someOtherDate addTimeInterval:60];
#endif
Is this at all possible using Xcode 4?
Upvotes: 2
Views: 1038
Reputation: 26558
You can use the exact same technique I described in CLLocation getDistanceFrom: vs distanceFromLocation:
Juste replace CLLocation
with NSDate
, getDistanceFrom:
with addTimeInterval:
and distanceFromLocation:
with dateByAddingTimeInterval:
in the instructions and you'll be able to always use dateByAddingTimeInterval:
no matter what SDK you are using and no matter what OS version you are running.
Upvotes: 0
Reputation: 14009
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
newDate = [someOtherDate dateByAddingTimeInterval:60];
#else
newDate = [someOtherDate addTimeInterval:60];
#endif
But it won't work with 10.5 if you build it with 10.6 SDK. need a runtime check as @Dave said.
Upvotes: 3
Reputation: 243156
How about +[NSDate dateWithTimeIntervalSinceNow:]
that's been around since 10.0?
Maybe instead of doing a compile-time check, you could do a runtime check:
if ([[NSDate class] instancesRespondToSelector:@selector(dateByAddingTimeInterval:)]) {
newDate = [someOtherDate dateByAddingTimeInterval:60];
} else {
newDate = [someOtherDate addTimeInterval:60];
}
Upvotes: 4