我试图在旧系统上支持10.9中新添加的NSColor方法. 10.9之前我在一个类别中有这些允许我统一Mac和iOS之间的代码.现在10.9有这些方法,无论我在哪里使用它,我都会得到奇怪的绘图.我想动态地将这些方法添加到较旧的运行时,并且我已经找到了几个使用class_addMethod来完成它的参考.问题是,即使addMethod返回成功,也不会调用方法.
NSColor * fColorWithWhite(id self,SEL _cmd,float white,float alpha) { return [NSColor colorWithDeviceWhite: white alpha: alpha]; } NSColor * fColorWithRedGreenBlue(id self,float red,float green,float blue,float alpha) { return [NSColor colorWithDeviceRed: red green: green blue: blue alpha: alpha]; } + (void)addLegacySupport { Class class = NSClassFromString(@"NSColor"); BOOL success = class_addMethod(class,@selector(colorWithWhite:alpha:),(IMP)fColorWithWhite,"@@:ff"); NSLog(@"colorWithWhite:alpha: - %i",success); success = class_addMethod(class,@selector(colorWithRed:green:blue:alpha:),(IMP)fColorWithRedGreenBlue,"@@:ffff"); NSLog(@"colorWithRed:green:blue:alpha: - %i",success); }
任何指针都将非常感激.