且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为 Objective-C/XCode 组织 #import 语句

更新时间:2022-12-30 09:17:39

<....> 语法确实只适用于框架.但这并不意味着您不应该创建一个框架来包含应用程序的核心逻辑.如果您:

The <....> syntax is indeed just for frameworks. That doesn't mean you shouldn't create a framework to contain the core logic of your application though. Often this is a useful thing to do if you:

a) 需要为想要调用应用程序逻辑方面的可加载包提供支持(包链接到框架,您的应用程序也是如此)b) 编写多个共享相同核心逻辑的应用

a) Need to provide support for loadable bundles that want to invoke aspects of your application logic (the bundle links to the framework, so does your application) b) Write multiple apps that share the same core logic

你的问题有点主观,你会得到两种观点的开发人员,但我遵循的约定是:

Your question is somewhat subjective and you will get developers who argues both ways, but a convention I follow is:

  1. 切勿在 .h 文件中导入类定义,除非您将其子类化.对 .h 中的所有内容使用前向 @class 指令.
  2. 仅将类定义导入到 .m 中,因为您需要在实现中使用该类.
  1. Never import class definitions in the .h file, unless you are subclassing it. Use forward @class directives for everything in the .h.
  2. Only import class definitions into a .m as you find you need to use that class in the implementation.

一般来说,.h 不需要访问其变量、方法参数或返回值的类定义.它只需要知道它们是类,这是 @class 允许你做的.它确实需要访问您要子类化、添加类别或(显然)为其实现协议的任何内容的类定义.

Generally speaking, the .h does not need access to the class definition of its ivars, method arguments or return values. It only needs to know that they are classes, which is what @class allows you to do. It does need access to the class definition of anything you're subclassing, adding a category to, or (obviously) implementing a protocol for.