Objective-C - 接口声明语法

时间:2014-12-27 04:20:23

标签: objective-c

Objective-C语法问题。括号内的“东西”是什么意思? (在这种情况下TemplateMethods?)

@interface CC3OpenGL (TemplateMethods)
// Methods declarations
@end

我发现这在Cocos3D中被大量使用,但不知道它意味着什么。

2 个答案:

答案 0 :(得分:2)

我正在扩大@Siriss答案,

当你想为另一个类创建一个类别时,你必须这样写。我不知道Cocos3D,所以我猜CC3OpenGLCocos3D中的一个类,您想要为该类创建一个类别并将其命名为TemplateMethods。< / p>

让我们举个例子,

@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此处。

答案 1 :(得分:1)

这是一个类别定义。类别是一种向您不拥有的类添加方法的方法,或者您执行但是特定于域的方法。您可以查看文档here