在Objective C中包含多个头的正确方法

时间:2022-10-25 15:08:27

So far I have been importing the required .h files in the .h files of class where it is required. I had read yesterday that this is the incorrect way to do it and I should forward declare the class in .h and then import it in .m However, I am confused what is the correct way to do this when it comes to large projects?

到目前为止,我一直在类的.h文件中导入所需的.h文件。我昨天读到这是不正确的方法,我应该转发.h中的类声明然后在.m中导入它但是,我很困惑在大型项目中这样做的正确方法是什么?

Since I will have a lot of imports in lots of files, this way it will make the code long in my opinion. Someone suggested me to create a BaseViewController class which subclasses UIViewController and add all my imports there, and all the UIViewController's that I will create will subclass BaseViewController and not UIViewController directly. This works, however I just want to know the correct way to do it.

由于我会在很多文件中导入大量内容,因此我认为这会使代码变长。有人建议我创建一个BaseViewController类,它继承UIViewController并在那里添加我的所有导入,而我将创建的所有UIViewController将直接子类化BaseViewController而不是UIViewController。这有效,但我只是想知道正确的方法。

Thanks in advance.

提前致谢。

4 个解决方案

#1


1  

  1. Simplest way to import header files in project is to create .pch file. Create .pch and add classes header in .pch file which you want to import most frequently. Then there is no need to import those headers any of the .h and .m. They will available for them automatically.
  2. 在项目中导入头文件的最简单方法是创建.pch文件。创建.pch并在.pch文件中添加要最频繁导入的类标题。然后就不需要导入任何.h和.m的头文件。它们将自动提供给他们。

Please refer this for how to create .pch file.

有关如何创建.pch文件,请参阅此处。

#2


1  

In Xcode, when you create a class, by default it will make .h and .m files of that class.

在Xcode中,当您创建一个类时,默认情况下它将生成该类的.h和.m文件。

These two files are used to separate between the public and private parts of the class.

这两个文件用于分隔类的公共部分和私有部分。

The .h file is a header file for public declarations of your class like an API, while the .m file is the private implementation.

.h文件是一个头文件,用于像API一样对类进行公共声明,而.m文件是私有实现。

You have to import somefile.h in somefile.m

你必须在somefile.m中导入somefile.h

#3


0  

你可以这样做.为每一个模块创建一个.h文件,把需要用到的类导入到里面,需要引用时直接引入这个模块的.h文件使用就好.这样不用写过多的.h文件,也避免了pch的导入文件过多的情况.

你可以这样做。为每一个模块创建一个.H文件,把需要用到的类导入到里面,需要引用时直接引入这个模块的.H文件使用就好。这样不用写过多的.H文件,也避免了PCH的导入文件过多的情况。

you can do like this. Creat one .h fiel for everyModuel,and import you want .h file to this .h . And import this .h file to where if you want . But if something .h file is base,please import they to .pch file.

你可以这样做为每个模块创建一个.h fiel,并将你想要的.h文件导入到这个.h。并将此.h文件导入到您想要的位置。但是如果某个.h文件是基础的,请将它们导入.pch文件。

In fact, I find it a little too rampant. But, hopefully useful to you.

事实上,我发现它有点过于猖獗。但是,希望对你有用。

#4


0  

Standard best practice for Objective-C is to forward declare classes in your .h header files and #import them in your .m implementation files. This reduces compile times and forces you to move dependencies to where they are actually required. The exception are base classes and protocols that your class inherits from. These need #imports in the header.

Objective-C的标准最佳实践是在.h头文件中转发声明类,并在.m实现文件中#import它们。这减少了编译时间并迫使您将依赖项移动到实际需要的位置。例外是您的类继承的基类和协议。这些在头文件中需要#imports。

The Google Style Guide for Objective-C specifies a recommended order in which headers should be included in a .m implementation file. This is partly to stop headers from depending on other headers being included before them. The order goes:

Objective-C的Google样式指南指定了一个建议的顺序,其中标题应包含在.m实现文件中。这部分是为了阻止标头依赖于它们之前包含的其他标头。订单如下:

  1. Related header (e.g. MyClass.h)
  2. 相关标题(例如MyClass.h)
  3. Operating system headers (e.g. Foundation/Foundation.h)
  4. 操作系统头文件(例如Foundation / Foundation.h)
  5. Language library headers (e.g. stdlib.h)
  6. 语言库头文件(例如stdlib.h)
  7. Groups of headers for other dependencies
  8. 其他依赖项的标头组

In general, many #imports and/or forward declarations making your code too long may be an indication that you should refactor to reduce dependencies between classes. Ideally the number of dependencies should be low, though ultimately this depends on the situation.

通常,许多#imports和/或前向声明会使代码过长,这可能表明您应该重构以减少类之间的依赖关系。理想情况下,依赖项的数量应该很少,但最终这取决于具体情况。

#1


1  

  1. Simplest way to import header files in project is to create .pch file. Create .pch and add classes header in .pch file which you want to import most frequently. Then there is no need to import those headers any of the .h and .m. They will available for them automatically.
  2. 在项目中导入头文件的最简单方法是创建.pch文件。创建.pch并在.pch文件中添加要最频繁导入的类标题。然后就不需要导入任何.h和.m的头文件。它们将自动提供给他们。

Please refer this for how to create .pch file.

有关如何创建.pch文件,请参阅此处。

#2


1  

In Xcode, when you create a class, by default it will make .h and .m files of that class.

在Xcode中,当您创建一个类时,默认情况下它将生成该类的.h和.m文件。

These two files are used to separate between the public and private parts of the class.

这两个文件用于分隔类的公共部分和私有部分。

The .h file is a header file for public declarations of your class like an API, while the .m file is the private implementation.

.h文件是一个头文件,用于像API一样对类进行公共声明,而.m文件是私有实现。

You have to import somefile.h in somefile.m

你必须在somefile.m中导入somefile.h

#3


0  

你可以这样做.为每一个模块创建一个.h文件,把需要用到的类导入到里面,需要引用时直接引入这个模块的.h文件使用就好.这样不用写过多的.h文件,也避免了pch的导入文件过多的情况.

你可以这样做。为每一个模块创建一个.H文件,把需要用到的类导入到里面,需要引用时直接引入这个模块的.H文件使用就好。这样不用写过多的.H文件,也避免了PCH的导入文件过多的情况。

you can do like this. Creat one .h fiel for everyModuel,and import you want .h file to this .h . And import this .h file to where if you want . But if something .h file is base,please import they to .pch file.

你可以这样做为每个模块创建一个.h fiel,并将你想要的.h文件导入到这个.h。并将此.h文件导入到您想要的位置。但是如果某个.h文件是基础的,请将它们导入.pch文件。

In fact, I find it a little too rampant. But, hopefully useful to you.

事实上,我发现它有点过于猖獗。但是,希望对你有用。

#4


0  

Standard best practice for Objective-C is to forward declare classes in your .h header files and #import them in your .m implementation files. This reduces compile times and forces you to move dependencies to where they are actually required. The exception are base classes and protocols that your class inherits from. These need #imports in the header.

Objective-C的标准最佳实践是在.h头文件中转发声明类,并在.m实现文件中#import它们。这减少了编译时间并迫使您将依赖项移动到实际需要的位置。例外是您的类继承的基类和协议。这些在头文件中需要#imports。

The Google Style Guide for Objective-C specifies a recommended order in which headers should be included in a .m implementation file. This is partly to stop headers from depending on other headers being included before them. The order goes:

Objective-C的Google样式指南指定了一个建议的顺序,其中标题应包含在.m实现文件中。这部分是为了阻止标头依赖于它们之前包含的其他标头。订单如下:

  1. Related header (e.g. MyClass.h)
  2. 相关标题(例如MyClass.h)
  3. Operating system headers (e.g. Foundation/Foundation.h)
  4. 操作系统头文件(例如Foundation / Foundation.h)
  5. Language library headers (e.g. stdlib.h)
  6. 语言库头文件(例如stdlib.h)
  7. Groups of headers for other dependencies
  8. 其他依赖项的标头组

In general, many #imports and/or forward declarations making your code too long may be an indication that you should refactor to reduce dependencies between classes. Ideally the number of dependencies should be low, though ultimately this depends on the situation.

通常,许多#imports和/或前向声明会使代码过长,这可能表明您应该重构以减少类之间的依赖关系。理想情况下,依赖项的数量应该很少,但最终这取决于具体情况。