为 Objective-C/XCode 组织 #import 语句 [英] Organize #import Statements for Objective-C/XCode

查看:53
本文介绍了为 Objective-C/XCode 组织 #import 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几个月的 Objective-C 编码,我完全理解什么时候需要 #import,import 语句如何级联(涟漪?),以及何时使用转发类.我知道如何聚合导入以将它们放在 <> 中而不是在引号中(尽管这可能仅适用于框架)...

After several months of coding in Objective-C, I completely understand when I need an #import, how import statements cascade (ripple?), and when to use forwarding classes. I do not know how to aggregate imports to get them inside of <> instead of in quotes (although maybe that's just for frameworks)...

问题是我把事情搞得一团糟.我来自 Java(和笨手笨脚的 IDE),所以我只添加我认为合适的导入.有时我将它们添加到界面中,但由于这通常不是必需的,我只是将它们添加到相关 .m 的顶部.

The problem is that I'm making a huge mess. I come from Java (and the heavy-handed IDE), so I just add imports as I see fit. Sometimes I add them to the interface, but since that's usually not necessary, I just add them to the top of the .m in question.

今天我开始思考:必须有一些关于如何组织这些东西的经验法则.事实上,由于Objective-C 是C 超集,一切 都有经验法则,但我不知道.我应该如何组织我的导入?特别是:

Today I started thinking: there must be some rules of thumb on how to organize this stuff. In fact, since Objective-C is a C superset, there are rules of thumb for everything, but I don't know them. How should I organize my imports? Particularly:

  • 我应该什么时候在 .m 中导入?
  • 我应该什么时候在 .h 中导入?
  • 我是否应该创建 .h 文件只是为了导入它们(即只有导入的头文件)?如果是这样,有什么关于组织的提示吗?
  • When should I import in the .m?
  • When should I import in the .h?
  • Should I create .h files just for the sake of importing them (i.e., header files that just have imports in them)? If so, any hints on organizing that?

这只是我想弄清楚的一个大概概念.

This is just a general idea of what I'm trying to figure out.

推荐答案

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

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.

这篇关于为 Objective-C/XCode 组织 #import 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆