本文介绍了山狮的条件类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Mountain Lion引入了新API ,其中一些已在项目中实现为类别.
Mountain Lion introduced new APIs, some of which we had implemented as categories in our project.
例如,我们有一个类别NSColor+CGColorAdditions
,它为NSColor
实现了CGColor
和colorWithCGColor:
.这些方法已在山狮"中添加.
For examples, we have a category NSColor+CGColorAdditions
that implemented CGColor
and colorWithCGColor:
for NSColor
. These methods have been added in Mountain Lion.
理想情况下,如果客户端操作系统早于Mountain Lion,我们希望使用这些类别,如果是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?
推荐答案
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_, "@@:@");
}
这篇关于山狮的条件类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!