问题
Mountain Lion introduced new APIs, some of which we had implemented as categories in our project.
For examples, we have a category NSColor+CGColorAdditions
that implemented CGColor
and colorWithCGColor:
for NSColor
. These methods have been added in Mountain Lion.
Ideally, we would like to use these categories if the client OS is older than Mountain Lion, and not use them if it's Mountain Lion. How can we do this? Or is there a better alternative?
回答1:
NSColor *_NSColor_colorWithCGColor_(Class self, SEL cmd, CGColorRef cgColor)
{
// make an NSColor outta `cgColor` and return it
return nsColor;
}
// inside some initialization code
if ([[NSColor class] respondsToSelector:@selector(colorWithCGColor:)]) {
// on ML, don't do anything
} else {
// older system, add your own category
class_addMethod(objc_getMetaClass("NSColor"), @selector(colorWithCGColor:), (IMP)_NSColor_colorWithCGColor_, "@@:@");
}
回答2:
I thought it might also be helpful to post what I did based on H2CO3's answer.
NSColor+CGColorAdditions.m
became:
static CGColorRef _NSColor_CGColor_(Class self, SEL cmd) {
return nil; // TODO: Do something
}
static NSColor* _NSColor_colorWithCGColor_(Class self, SEL cmd, CGColorRef aColor) {
return nil; // TODO: Do something
}
__attribute__((constructor))
static void initialize_NSColor_CGColorAdditions() {
if (![[NSColor class] respondsToSelector:@selector(colorWithCGColor:)]) {
class_addMethod(objc_getMetaClass("NSColor"), @selector(colorWithCGColor:), (IMP)_NSColor_colorWithCGColor_, "@@:@");
}
if (![[NSColor class] instancesRespondToSelector:@selector(CGColor)]) {
class_addMethod(objc_getClass("NSColor"), @selector(CGColor), (IMP)_NSColor_CGColor_, "@@:");
}
}
来源:https://stackoverflow.com/questions/11950173/conditional-categories-in-mountain-lion