Conditional categories in Mountain Lion

感情迁移 提交于 2019-12-17 20:26:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!