在目标c中导入标题 [英] Importing header in objective c

查看:124
本文介绍了在目标c中导入标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-c中,当我们按照约定将一个类的对象用于另一个类时,我们应该在.h文件中转发声明类,即 @class classname; 。并且应该在.m文件中导入头文件,即 #importheader.h
但是如果我们在.h中导入头文件,那么我们不必再在.m文件中导入它。
那么这个大会背后的原因是什么?哪种方法有效?

In Objective-c when we using object of one class into another class by convention we should forward declare the class in .h file, i.e. @class classname;. And should import the header file in .m file, i.e. #import "header.h". But if we import the header file in .h then we don't have to import it again in .m file . So what is the reason behind this convention? Which is efficient way?

推荐答案


那么这个约定背后的原因是什么?

So what is the reason behind this convention?

你应该在可能的情况下支持前向声明( @class MONClass; ),因为编译器需要知道typename在使用之前是一个objc类,并且因为 #import 可以拖入大量其他标题(例如整个框架/库),严重扩展和复杂化你的依赖关系并增加你的构建时间。

You should favor forward declarations (@class MONClass;) where possible because the compiler needs to know a typename is an objc class before it is used, and because an #import can drag in a ton of other headers (e.g. entire frameworks/libraries), seriously expanding and complicating your dependencies and increasing your build times.


哪种方式有效?

Which is efficient way?

前瞻性声明。如果您正确执行此操作,您的构建,重建和索引将 更快。

Forward declarations. Your builds, rebuilds, and indexing will be much faster if you do this correctly.

这篇关于在目标c中导入标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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