山狮介绍 new APIs ,其中一些我们已经在我们的项目中实现为类别。
例如,我们有一个类别NSColor+CGColorAdditions
,它为NSColor
实现了CGColor
和colorWithCGColor:
。这些方法已添加到 Mountain Lion 中。
理想情况下,如果客户端操作系统早于 Mountain Lion,我们希望使用这些类别,如果是 Mountain Lion,则不使用它们。我们应该怎么做?或者有更好的选择吗?
最佳答案
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_, "@@:@");
}
关于objective-c - Mountain Lion 中的条件类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950173/