【发布时间】:2014-12-27 04:20:22
【问题描述】:
Objective-C 语法问题。括号内的“事物”是什么意思? (在这种情况下是TemplateMethods?)
@interface CC3OpenGL (TemplateMethods)
// Methods declarations
@end
我看到这个在 Cocos3D 中被大量使用,但不知道它是什么意思。
【问题讨论】:
标签: objective-c
Objective-C 语法问题。括号内的“事物”是什么意思? (在这种情况下是TemplateMethods?)
@interface CC3OpenGL (TemplateMethods)
// Methods declarations
@end
我看到这个在 Cocos3D 中被大量使用,但不知道它是什么意思。
【问题讨论】:
标签: objective-c
我正在扩展@Siriss 的答案,
当你想为另一个类创建一个类别时,你必须这样写。我不知道Cocos3D,所以我猜CC3OpenGL 是Cocos3D 中的一个类,您想为该类创建一个类别并将其命名为TemplateMethods。
举个例子,
@interface UILabel (OtherMethods)
// Methods declarations
- (CGSize) getLabelSize;
@end
在这里,我们创建了一个UILabel 的类别并将其命名为OtherMethods。我们可以#import点赞,#import "UILabel+OtherMethods.h访问它的方法。
所以,现在您将拥有UILabel 的方法,您可以像访问UILabel 的其他方法一样访问它。
UILabel *lbl = [UILabel new];
lbl.frame = CGRectMake:(10,10,300,100);
[lbl setTextAlignment:NSTextAlignmentRight]; //normal method
[self.view addSubview:lbl];
CGSize lblSize = [lbl getLabelSize]; //category method
要了解更多信息,请点击此处read documentation。
【讨论】:
这是一个类别定义。类别是一种向您不拥有的类或您拥有但特定于域的类添加方法的方法。您可以查看文档here。
【讨论】: