开发者

Xcode - preprocessor directive check which SDK is being used

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 dateByAddingTimeInter开发者_开发百科val:60];
#else
newDate= [someOtherDate addTimeInterval:60];
#endif

Is this at all possible using Xcode 4?


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];
}


#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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜