Objective-C中#import和#include有什么区别? [英] What is the difference between #import and #include in Objective-C?

查看:148
本文介绍了Objective-C中#import和#include有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C中#import和#include之间有什么区别,有时你应该使用另一个吗?一个被弃用了吗?

What are the differences between #import and #include in Objective-C and are there times where you should use one over the other? Is one deprecated?

我正在阅读以下教程: http://www.otierney.net/objective-c.html#preamble 及其关于#import和#include的段落似乎自相矛盾,或者至少不清楚。

I was reading the following tutorial: http://www.otierney.net/objective-c.html#preamble and its paragraph about #import and #include seems to contradict itself or at least is unclear.

推荐答案

#import指令作为#include的改进版本添加到Objective-C中。然而,它是否得到改善仍然是一个争论的问题。 #import确保文件只被包含一次,这样你就不会有递归包含的问题。但是,大多数不错的头文件都可以保护自己不受此影响,所以它并没有那么大的好处。

The #import directive was added to Objective-C as an improved version of #include. Whether or not it's improved, however, is still a matter of debate. #import ensures that a file is only ever included once so that you never have a problem with recursive includes. However, most decent header files protect themselves against this anyway, so it's not really that much of a benefit.

基本上,由你来决定你要使用哪个。我倾向于#import标头用于Objective-C事物(比如类定义等)和#include标准C我需要的东西。例如,我的一个源文件可能如下所示:

Basically, it's up to you to decide which you want to use. I tend to #import headers for Objective-C things (like class definitions and such) and #include standard C stuff that I need. For example, one of my source files might look like this:

#import <Foundation/Foundation.h>

#include <asl.h>
#include <mach/mach.h>

这篇关于Objective-C中#import和#include有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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