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

查看:29
本文介绍了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 指令被添加到 Objective-C 中,作为 #include 的改进版本.然而,它是否有所改善,仍然是一个有争议的问题.#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.

基本上,由您决定要使用哪个.我倾向于为 Objective-C 事物(如类定义等)#import 标头,并#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天全站免登陆