为什么标准 Xcode 模板中没有导入 Foundation.h?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6083475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:05:27  来源:igfitidea点击:

Why is Foundation.h not imported in the standard Xcode templates?

iphoneobjective-cxcodeios

提问by NSExplorer

I just noticed that when I create iPhone application using standard templates in Xcode (View Based Application, Window based application etc) the header files only import UIKit.h (and not Foundation.h). So if the Foundation.h file is not imported, how can one use Foundation classes like NSString, NSArray etc in the program ? Shouldn't it be throwing an error?

我刚刚注意到,当我使用 Xcode 中的标准模板(基于视图的应用程序、基于窗口的应用程序等)创建 iPhone 应用程序时,头文件只导入 UIKit.h(而不是 Foundation.h)。那么如果Foundation.h文件没有被导入,如何在程序中使用NSString、NSArray等Foundation类呢?它不应该抛出错误吗?

回答by BoltClock

It is imported in the precompiled header file (extension .pch) in your project.

它被导入到项目的预编译头文件(扩展名.pch)中。

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
#endif

As to why UIKit.happears to have two import lines per file since it also shows up above, though, I'm not too sure. It shouldn't matter anyway, as the second #importline won't cause UIKit to be included again if it's already included in the precompiled header file.

至于为什么UIKit.h每个文件似乎有两个导入行,因为它也显示在上面,不过,我不太确定。无论如何都没有关系,因为#import如果 UIKit 已经包含在预编译头文件中,第二行不会导致再次包含 UIKit。

回答by jscs

If you take a look at some of the individual UIKit headers, they import Foundation themselves. As BoltClock noted, it's also in the standard .pch

如果您查看一些单独的 UIKit 标头,它们会自己导入 Foundation。正如 BoltClock 所指出的,它也在标准 .pch 中

The reason that it canbe put in all these places is mentioned in "The Objective-C Programming Language":

《The Objective-C Programming Language》中提到了它可以放在所有这些地方的原因:

This directive is identical to #include, except that it makes sure that the same file is never included more than once. It's therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.

该指令与#include 相同,不同之处在于它确保同一文件不会被多次包含。因此,它是首选,并在基于 Objective-C 的文档中的代码示例中代替 #include 使用。

You want to make sure that every file that needs access to the symbols defined in Foundation has them. The reason it's repeated is that it helps with reading and understanding the code; it says "This code relies on the stuff that's in this header".

您要确保需要访问 Foundation 中定义的符号的每个文件都有它们。之所以重复,是因为它有助于阅读和理解代码;它说“此代码依赖于此标题中的内容”。

回答by idz

It looks like the template files are designed to work with or without precompiled headers, so they import the header files they use. If you create a subclass of NSObjectthe template file willimport Foundation. If you create a subclass of a UIKitobject the UIKitheader will be imported.

看起来模板文件旨在使用或不使用预编译头文件,因此它们导入它们使用的头文件。如果创建NSObject模板文件的子类,导入Foundation. 如果您创建UIKit对象的子类,UIKit则将导入标头。

OK, so why can you use Foundationclasses like NSStringwhen only UIKitis imported. Well nearly all of the UIKitheaders import Foundation.h!

好的,那么为什么您可以使用仅导入时之Foundation类的类。几乎所有的标题都导入了!NSStringUIKitUIKitFoundation.h

Hope this make sense.

希望这是有道理的。