xcode 为什么@implementation 上面有@interface?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11594391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:57:57  来源:igfitidea点击:

Why is there @interface above @implementation?

objective-ciosxcodeios5xcode4.4

提问by Matej

I am wondering why there is twice @interface. One in class.hand other in class.m. For example:

我想知道为什么有两次@interface。一个在class.h,另一个在class.m。例如:

TestTableViewController.h:

TestTableViewController.h

#import <UIKit/UIKit.h>

@interface TestTableViewController : UITableViewController

@end

and (automatically generated) class.mi find:

并且(自动生成)class.m我发现:

#import "TestTableViewController.h"

@interface TestTableViewController ()

@end

@implementation TestTableViewController

... methods delegated from UITable delegates

@end

So my question is, what the @interface TestTableViewController ()in the .mfile is about. Why it is there? Do I need it?

所以我的问题是,什么@interface TestTableViewController ().m的文件是什么。为什么它在那里?我需要吗?

Thanks in advance

提前致谢

回答by

The second @interfacedirective is in the implementation file (.m) -- you can infer from it that it's meant for declaring stuff that the creator of the class didn't want to expose to the user of the class. This usually means private and/or internal methods and properties. Also note that there are two types of doing this. The one (which you see here) is called a "class extension" and it's denoted by an empty pair of parentheses:

第二个@interface指令在实现文件 (.m) 中——您可以从中推断出它用于声明类的创建者不想向类的用户公开的内容。这通常意味着私有和/或内部方法和属性。另请注意,这样做有两种类型。一个(你在这里看到的)被称为“类扩展”,它由一对空括号表示:

@interface MyClass ()

This one is particularily important because you can use this to add additional instance variables to your class.

这个特别重要,因为您可以使用它向您的类添加额外的实例变量。

The second one, called a "category", is indicated by a non-empty pair of parentheses, enclosing the name of the category, like this:

第二个称为“类别”,由一对非空括号表示,包含类别名称,如下所示:

@interface MyClass (CategoryName)

and it's also used to extend the class. You can't add instance variables to a class using categories, but you can have multiple categories for the same class, that's the reason why it's mainly used to extend system/framework classes for which you don't have the source code -- so a category, in this sense, is the exact opposite of the class extension.

它也用于扩展类。您不能使用类别向类添加实例变量,但是您可以为同一个类设置多个类别,这就是它主要用于扩展您没有源代码的系统/框架类的原因——所以从这个意义上说,类别与类扩展正好相反。

回答by dcoder

The second "interface" defines an extension for the "TestTableViewController" class, which is not visible to someone who only imports the h file. This is the de-facto way for creating private methods in objective C.

第二个“接口”定义了“TestTableViewController”类的扩展,对于只导入 h 文件的人是不可见的。这是在目标 C 中创建私有方法的事实上的方式。

回答by Sandro Meier

The interface in the TestTableViewController.hfile is the declaration of a class extension. There are 2 round brackets that show this. The syntax is the same as for writing a category for a class. But in this case it's used to declare some sort of private methods the author does not want to expose in the header file

TestTableViewController.h文件中的接口是类扩展的声明。有 2 个圆括号显示了这一点。语法与为类编写类别相同。但在这种情况下,它用于声明作者不想在头文件中公开的某种私有方法

A normal category interface looks like this:

一个正常的分类界面是这样的:

@interface TestTableViewController (Your_Category_Name)
- (void)doSomething;
@end

And the corresponding implementation:

以及相应的实现:

@implementation TestTableViewController (Your_Category_Name)
-(void)doSomething {
// Does something...
}
@end

In your example there is no category name specified, so it just extends the class and you can implement the method in the normal implementation.

在您的示例中,没有指定类别名称,因此它只是扩展了类,您可以在正常实现中实现该方法。

Normally this technique is used to "hide" methods. They are not declared in the header file and are not visible if you only import the .h file.

通常,此技术用于“隐藏”方法。它们未在头文件中声明,并且如果您只导入 .h 文件则不可见。

回答by DrummerB

In there you can declare private methods and properties that you only want to use in your class, but not expose to other classes.

在那里你可以声明你只想在你的类中使用但不公开给其他类的私有方法和属性。