Why do we have the '@interface ViewController' line in both the implementation and header file for a viewcontroller in xcode?
为什么我们在xcode中的viewcontroller的实现和头文件中都有'@interface ViewController'行?
2 个解决方案
#1
7
It's to do with the visibility of the contents of the @interface
. When it's contained within the header file, it's available for other classes to see when they import the header file. When it's contained within the implementation file, the contents are only available to that implementation file alone. Typically when it's declared within the implementation file, it is done via a class extension (i.e. @interface ClassName ()
, the ()
denotes a class extension/anonymous category), although a named category can be used if so desired.
这与@interface内容的可见性有关。当它包含在头文件中时,它可供其他类查看何时导入头文件。当它包含在实现文件中时,内容仅可用于该实现文件。通常,当它在实现文件中声明时,它通过类扩展(即@interface ClassName()完成,()表示类扩展/匿名类别),尽管如果需要可以使用命名类别。
There are a few reasons why this is done. The main one is to define private instance variables or properties. You don't want these to be exposed to everyone who imports the header file, but you need a place to store internal information. For example, this will allow m_isActive
to be used within the implementation only:
这样做有几个原因。主要是定义私有实例变量或属性。您不希望向导入头文件的所有人公开这些内容,但您需要一个存储内部信息的位置。例如,这将允许m_isActive仅在实现中使用:
@interface Class () {
BOOL m_isActive;
}
You can also override readonly properties declared in the header file, so that the implementation file has readwrite
access to it when using dot notation. For example:
您还可以覆盖头文件中声明的只读属性,以便在使用点表示法时实现文件具有对其的读写访问权限。例如:
Header:
标题:
@interface Class
@property (nonatomic, readonly) NSString* name;
@end
Implementation:
执行:
@interface Class ()
@property (nonatomic) NSString* name;
@end
@implementation Class
...
self.name = @"WDUK"; // This is allowed, as the class extension has overridden the readonly attribute via a redeclaration of the property
...
@end
Another popular use is to privately declare that you conform to particular protocols, which is an implementation detail and does not need to be exposed within the public header file. For example, when the implementation uses an object that requires it to be a delegate, and you don't want to pollute the header file with a protocol that isn't used outside the class.
另一个流行的用法是私下声明您符合特定协议,这是一个实现细节,不需要在公共头文件中公开。例如,当实现使用一个需要它作为委托的对象时,并且您不希望使用在类外部未使用的协议污染头文件。
Other uses (which have been left in the dark with recent LLVM/Clang improvements) were to define private methods. This is no longer required, as the compiler will look for methods not declared within the respective header file, and assume they are private to the class and declare them itself.
其他用途(在最近的LLVM / Clang改进中一直存在)是定义私有方法。这不再是必需的,因为编译器将查找未在相应头文件中声明的方法,并假设它们对类是私有的并且自己声明它们。
The key part to take from all this, is that anything within @interface
within the header file (Except instance variables defined there via @private
or @protected
) are public, and anything within the implementation file is inherently private.
从这一切中得到的关键部分是,头文件中@interface内的任何内容(除了通过@private或@protected定义的实例变量除外)都是公共的,实现文件中的任何内容本质上都是私有的。
#2
2
The one in the header file defines the public interface.
头文件中的那个定义了公共接口。
The one in the implementation file is a class extension that extends the public interface with any private implementation details.
实现文件中的一个是类扩展,它使用任何私有实现细节扩展公共接口。
#1
7
It's to do with the visibility of the contents of the @interface
. When it's contained within the header file, it's available for other classes to see when they import the header file. When it's contained within the implementation file, the contents are only available to that implementation file alone. Typically when it's declared within the implementation file, it is done via a class extension (i.e. @interface ClassName ()
, the ()
denotes a class extension/anonymous category), although a named category can be used if so desired.
这与@interface内容的可见性有关。当它包含在头文件中时,它可供其他类查看何时导入头文件。当它包含在实现文件中时,内容仅可用于该实现文件。通常,当它在实现文件中声明时,它通过类扩展(即@interface ClassName()完成,()表示类扩展/匿名类别),尽管如果需要可以使用命名类别。
There are a few reasons why this is done. The main one is to define private instance variables or properties. You don't want these to be exposed to everyone who imports the header file, but you need a place to store internal information. For example, this will allow m_isActive
to be used within the implementation only:
这样做有几个原因。主要是定义私有实例变量或属性。您不希望向导入头文件的所有人公开这些内容,但您需要一个存储内部信息的位置。例如,这将允许m_isActive仅在实现中使用:
@interface Class () {
BOOL m_isActive;
}
You can also override readonly properties declared in the header file, so that the implementation file has readwrite
access to it when using dot notation. For example:
您还可以覆盖头文件中声明的只读属性,以便在使用点表示法时实现文件具有对其的读写访问权限。例如:
Header:
标题:
@interface Class
@property (nonatomic, readonly) NSString* name;
@end
Implementation:
执行:
@interface Class ()
@property (nonatomic) NSString* name;
@end
@implementation Class
...
self.name = @"WDUK"; // This is allowed, as the class extension has overridden the readonly attribute via a redeclaration of the property
...
@end
Another popular use is to privately declare that you conform to particular protocols, which is an implementation detail and does not need to be exposed within the public header file. For example, when the implementation uses an object that requires it to be a delegate, and you don't want to pollute the header file with a protocol that isn't used outside the class.
另一个流行的用法是私下声明您符合特定协议,这是一个实现细节,不需要在公共头文件中公开。例如,当实现使用一个需要它作为委托的对象时,并且您不希望使用在类外部未使用的协议污染头文件。
Other uses (which have been left in the dark with recent LLVM/Clang improvements) were to define private methods. This is no longer required, as the compiler will look for methods not declared within the respective header file, and assume they are private to the class and declare them itself.
其他用途(在最近的LLVM / Clang改进中一直存在)是定义私有方法。这不再是必需的,因为编译器将查找未在相应头文件中声明的方法,并假设它们对类是私有的并且自己声明它们。
The key part to take from all this, is that anything within @interface
within the header file (Except instance variables defined there via @private
or @protected
) are public, and anything within the implementation file is inherently private.
从这一切中得到的关键部分是,头文件中@interface内的任何内容(除了通过@private或@protected定义的实例变量除外)都是公共的,实现文件中的任何内容本质上都是私有的。
#2
2
The one in the header file defines the public interface.
头文件中的那个定义了公共接口。
The one in the implementation file is a class extension that extends the public interface with any private implementation details.
实现文件中的一个是类扩展,它使用任何私有实现细节扩展公共接口。