How detect SDK version

I have the following code:

@implementation NSWindow (Coordination)

- (NSPoint)convertPointFromScreen:(NSPoint)point
{
    NSRect rect = NSMakeRect(point.x, point.y, 1, 1);
    return [self convertRectFromScreen:rect].origin;
}

I get a compiliation warning:

Category is implementing a method which will also be implemented by its primary class


I know Swift has builtin preprocessor directives to detect SDK version, but don't know how to do this in objc. Any suggestions?

  • I am disappointed that there is no actual answer to the question in the title. I need an answer to this and this is the first hit on Google. Yet, all that I see here is suggestions for solving the specific issue but not the question in the title.

    To answer Quinn's question: In my case, I need to keep my code compatible with various SDK versions (for various reasons that are valid but shouldn't need to be justified here), so I need to add conditional code depending on the SDK version. And since it's a compile time issue, I cannot use @available() here, either.

    So I really need to know which SDK I'm compiling under.

Add a Comment

Replies

How does detecting the SDK help here? If your deployment target is macOS 10.12 or later, you can just delete this code. And if your deployment target includes older systems, you need to decide at runtime whether to use your compatibility code or not.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The easiest solution is to just rename your method so that it doesn't conflict.


The next best method is to use "respondsToSelector" to see if an object implements a given selector, which you can create from a string. Sometimes, however, you could get a method with and early, buggy, or incomplete implementation.


AppKit has an API to detection versions, but it is incomplete and difficult to use. It officially stops at 10.12.


There are a number of ways to check the OS version at runtime. Do it once early on and save the result to decide which set of code to run. Or, even better, just use code that is going to work on any version. Don't worry if you are duplicating something that might be implemented in a more recent system.

Yes, renaming the method is an easy way. The reason why I asked this question is that I want to know if objc has something similar with Swift #available language construct. This way I will expand my knowlege a little bit.

Obj-C has "if (@available(…))" corresponding to Swift's "if #available(…)". Details on this web page:


clang.llvm.org/docs/LanguageExtensions.html#objective-c-available

To those who need an answer on how to conditionally compile depending on the used SDK version, here's what I found to work:

#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 120000
  // ... compiles only when using the macOS12.0 SDK (or later) that comes with Xcode 13.1
#endif