一个Objective-C类扩展头-用于什么?

时间:2022-09-07 07:41:54

I'm familiar with Objective-C class extensions in class main implementation file, but curious for what the new Xcode 4.4 "An Objective-C class extension header" file template is intended for?

我熟悉类main实现文件中的Objective-C类扩展,但是好奇新的Xcode 4.4“Objective-C类扩展头”文件模板是用来做什么?

2 个解决方案

#1


22  

Class extensions must be implemented in the main @implementation block, but the declaration can be anywhere.

类扩展必须在主@implementation块中实现,但是声明可以在任何地方实现。

Extensions are used to add something to the class interface that you do not want to be public, and therefore cannot put in the public class declaration header.

扩展用于向类接口添加不希望是公共的内容,因此不能将其放入公共类声明头。

Declaring the extension in the same file of the implementation, which you are familiar with, is used when the extension is only used by the class implementation itself.

当扩展仅由类实现本身使用时,使用您熟悉的实现的同一文件中声明扩展。

Declaring the extension in a separate header, which is what the template is for, is useful when you develop a framework. The extension header will not be part of the set of public headers, but will be used internally by more than one implementation file of the framework.

在开发框架时,在一个单独的头中声明扩展,这是模板的作用所在。扩展头不会是公共标头的一部分,但将在内部使用框架的多个实现文件。

You can think of it as private to framework instead of private to class.

您可以将它看作是框架的私有属性,而不是类的私有属性。

#2


9  

Another reason is that a Class Extension Header file is useful in the case you are using Unit Test to test your class, putting class extension in a separate header file allow you to import the the header containing private methods into the implementation of the class that needs those private methods and into the unit test file that you use to test the class ...

另一个原因是,扩展一个类头文件是有用的对于你使用单元测试来测试类,将在一个单独的头文件类扩展允许您导入头包含私有方法的实现类,需要这些私有方法和单元测试文件,您使用测试类…

This is a very good addition(the opportunity to test private methods in the unit test) to Objective-C in my opinion, it is a thing that you can't do at the moment with JUnit in Java .

这是对Objective-C的一个很好的补充(在单元测试中测试私有方法的机会)。

#1


22  

Class extensions must be implemented in the main @implementation block, but the declaration can be anywhere.

类扩展必须在主@implementation块中实现,但是声明可以在任何地方实现。

Extensions are used to add something to the class interface that you do not want to be public, and therefore cannot put in the public class declaration header.

扩展用于向类接口添加不希望是公共的内容,因此不能将其放入公共类声明头。

Declaring the extension in the same file of the implementation, which you are familiar with, is used when the extension is only used by the class implementation itself.

当扩展仅由类实现本身使用时,使用您熟悉的实现的同一文件中声明扩展。

Declaring the extension in a separate header, which is what the template is for, is useful when you develop a framework. The extension header will not be part of the set of public headers, but will be used internally by more than one implementation file of the framework.

在开发框架时,在一个单独的头中声明扩展,这是模板的作用所在。扩展头不会是公共标头的一部分,但将在内部使用框架的多个实现文件。

You can think of it as private to framework instead of private to class.

您可以将它看作是框架的私有属性,而不是类的私有属性。

#2


9  

Another reason is that a Class Extension Header file is useful in the case you are using Unit Test to test your class, putting class extension in a separate header file allow you to import the the header containing private methods into the implementation of the class that needs those private methods and into the unit test file that you use to test the class ...

另一个原因是,扩展一个类头文件是有用的对于你使用单元测试来测试类,将在一个单独的头文件类扩展允许您导入头包含私有方法的实现类,需要这些私有方法和单元测试文件,您使用测试类…

This is a very good addition(the opportunity to test private methods in the unit test) to Objective-C in my opinion, it is a thing that you can't do at the moment with JUnit in Java .

这是对Objective-C的一个很好的补充(在单元测试中测试私有方法的机会)。