Occasionally you might have the need to determine what version of the Application Kit (or Foundation) framework you’re using at runtime. You might, for example, have code that uses new features in Cocoa and have been putting #ifdef
statements in place to control these workarounds. However, others may take this code from one build and compile and run it on another where the feature may not yet be available.
There are several ways to check at runtime for new features in the versions of the Cocoa frameworks your application is linked against. You can dynamically look for a given class or method (for example, by using the instancesRespondToSelector:
method) and, if it isn’t there, follow a different code path. Another way is to use the the global framework-version constants provided by the Application Kit and Foundation frameworks.
The Application Kit (in NSApplication.h
) declares the NSAppKitVersionNumber
constant, which you can use to detect different versions of the Application Kit framework:
/* The version of the AppKit framework */ |
APPKIT_EXTERN double NSAppKitVersionNumber; |
#define NSAppKitVersionNumber10_0 577 |
#define NSAppKitVersionNumber10_1 620 |
#define NSAppKitVersionNumber10_2 663 |
#define NSAppKitVersionNumber10_2_3 663.6 |
#define NSAppKitVersionNumber10_3 743 |
#define NSAppKitVersionNumber10_3_2 743.14 |
#define NSAppKitVersionNumber10_3_3 743.2 |
#define NSAppKitVersionNumber10_3_5 743.24 |
You can compare against this value to determine which version of the Application Kit your code is running against. One typical approach is to floor the value of the global constant and check the result against the constants declared in NSApplication.h
. For example:
if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_0) { |
/* On a 10.0.x or earlier system */ |
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_1) { |
/* On a 10.1 - 10.1.x system */ |
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_2) { |
/* On a 10.2 - 10.2.x system */ |
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_3) { |
/* On a 10.3 - 10.3.x system */ |
} else { |
/* Tiger or later system */ |
} |
Similarly, Foundation (in NSObjCRuntime.h
) declares the NSFoundationVersionNumber
global constant and specific values for each version.
Some individual headers for other objects and components may also declare the versions numbers for NSAppKitVersionNumber
where some bug fix or functionality is available in a given update.
#define NSAppKitVersionWithSuchAndSuchBadBugFix 582.1 |
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-11-07)