ios 为什么在 Xcode 6 中没有自动创建 ProjectName-Prefix.pch?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24158648/
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
Why isn't ProjectName-Prefix.pch created automatically in Xcode 6?
提问by Wei
Why isn't ProjectName-Prefix.pch
created automatically in Xcode 6
?
为什么不在 中ProjectName-Prefix.pch
自动创建Xcode 6
?
Is the precompile header no longer needed ?
不再需要预编译头吗?
Where should I write the code that was in ProjectName-Prefix.pch
before ?
我应该在哪里写之前的代码ProjectName-Prefix.pch
?
采纳答案by Rob Napier
I suspect because of modules, which remove the need for the #import <Cocoa/Cocoa.h>
.
我怀疑是因为模块,它消除了对#import <Cocoa/Cocoa.h>
.
As to where to put code that you would put in a prefix header, there is no code you should put in a prefix header. Put your imports into the files that need them. Put your definitions into their own files. Put your macros...nowhere. Stop writing macros unless there is no other way (such as when you need __FILE__
). If you do need macros, put them in a header and include it.
至于将要放在前缀头中的代码放在哪里,没有任何代码应该放在前缀头中。将您的导入放入需要它们的文件中。将您的定义放入它们自己的文件中。把你的宏......无处可去。停止编写宏,除非没有其他方法(例如当您需要时__FILE__
)。如果您确实需要宏,请将它们放在标题中并包含它。
The prefix header was necessary for things that are huge and used by nearly everything in the whole system (like Foundation.h
). If you have something that huge and ubiquitous, you should rethink your architecture. Prefix headers make code reuse hard, and introduce subtle build problems if any of the files listed can change. Avoid them until you have a serious build time problem that you can demonstrate is dramatically improved with a prefix header.
前缀标头对于整个系统中几乎所有东西都使用的巨大事物(如Foundation.h
)是必需的。如果您拥有如此庞大且无处不在的东西,您应该重新考虑您的架构。前缀头使代码重用变得困难,如果列出的任何文件可以更改,则会引入微妙的构建问题。避免使用它们,直到您遇到严重的构建时间问题,您可以证明使用前缀标头显着改善了该问题。
In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.
在这种情况下,您可以创建一个并将其传递给 clang,但这是一个好主意的情况非常罕见。
EDIT: To your specific question about a HUD you use in all your view controllers, yes, you should absolutely import it into every view controller that actually uses it. This makes the dependencies clear. When you reuse your view controller in a new project (which is common if you build your controllers well), you will immediately know what it requires. This is especiallyimportant for categories, which can make code very hard to reuse if they're implicit.
编辑:关于您在所有视图控制器中使用的 HUD 的具体问题,是的,您绝对应该将它导入到实际使用它的每个视图控制器中。这使得依赖关系变得清晰。当你在一个新项目中重用你的视图控制器(如果你很好地构建了你的控制器,这很常见),你会立即知道它需要什么。这对于类别尤其重要,如果它们是隐式的,这会使代码很难重用。
The PCH file isn't there to get rid of listing dependencies. You should still import UIKit.h
or Foundation.h
as needed, as the Xcode templates do. The reason for the PCH is to improve build times when dealing with really massive headers (like in UIKit).
PCH 文件不是为了摆脱列表依赖关系。您仍然应该导入UIKit.h
或Foundation.h
根据需要导入,就像 Xcode 模板所做的那样。使用 PCH 的原因是在处理真正庞大的头文件(如 UIKit 中)时缩短构建时间。
回答by Yedidya Reiss
Without the question if it is proper or not, you can add PCH file manually:
没有问题是否正确,您可以手动添加PCH文件:
Add new PCH file to the project: New file > Other > PCH file.
At the Target's Build Settingsoption, set the value of Prefix Headerto your PCH file name, with the project name as prefix (i.e. for project named
TestProject
and PCH file namedMyPrefixHeaderFile
, add the valueTestProject/MyPrefixHeaderFile.pch
to the plist).TIP:You can use things like
$(SRCROOT)
or$(PROJECT_DIR)
to get to the path of where you put the.pch
in the project.At the Target's Build Settingsoption, set the value of Precompile Prefix Headerto
YES
.
向项目添加新的 PCH 文件:新建文件 > 其他 > PCH 文件。
在 Target 的Build Settings选项中,将Prefix Header的值设置为您的 PCH 文件名,以项目名称作为前缀(即对于名为 的项目
TestProject
和名为PCH 的文件MyPrefixHeaderFile
,将值添加TestProject/MyPrefixHeaderFile.pch
到 plist)。提示:您可以使用诸如
$(SRCROOT)
或 之类的东西$(PROJECT_DIR)
来到达您.pch
在项目中放置的位置的路径。在 Target 的Build Settings选项中,将Precompile Prefix Header的值设置为
YES
.
回答by Prakash Raj
You need to create own PCH file
Add New file -> Other-> PCH file
您需要创建自己的 PCH 文件
添加New file -> Other-> PCH file
Then add the path of this PCH file to your build setting->prefix header->path
然后将此 PCH 文件的路径添加到您的构建中 setting->prefix header->path
($(SRCROOT)/filename.pch)
($(SRCROOT)/文件名.pch)
回答by Slemon
I'll show you with a pic!
我给你看一张图!
Add a new File
Go to Project/Build Setting/APPl LLVM 6.0-Language
添加新文件
转到项目/构建设置/APPl LLVM 6.0-语言
回答by Subham93
To add .pch file-
添加 .pch 文件 -
1) Add new .pch file to your project->New file->other->PCH file
1) 将新的 .pch 文件添加到您的项目->新文件->其他->PCH 文件
2) Goto your project's build setting.
2)转到您项目的构建设置。
3) Search "prefix header". You can find that under Apple LLVM.
3) 搜索“前缀标题”。您可以在 Apple LLVM 下找到它。
4) Paste this in the field $(SRCROOT)/yourPrefixHeaderFileName.pch
4) 将此粘贴到该字段中 $(SRCROOT)/yourPrefixHeaderFileName.pch
5) Clean and build the project. That's it!!!
5) 清理并构建项目。就是这样!!!
回答by mgm
If you decide to add a .pch file manually and you want to use Objective-C just like before xCode 6 you will also have to import UIKit and Foundation frameworks in the .pch file. Otherwise you will have to import these frameworks manually in each header file. You can add the following code anyway as it tests for the language used:
如果你决定手动添加一个 .pch 文件并且你想像在 xCode 6 之前一样使用 Objective-C,你还必须在 .pch 文件中导入 UIKit 和 Foundation 框架。否则,您将不得不在每个头文件中手动导入这些框架。您可以在测试所用语言时添加以下代码:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
回答by Arash Zeinoddini
Use :
用 :
$(PROJECT_DIR)/Project name/PrefixHeader.pch
$(PROJECT_DIR)/项目名称/PrefixHeader.pch
回答by Hitesh Vaghela
For add new PCH file follow bellow steps :
要添加新的 PCH 文件,请按照以下步骤操作:
(1) Add New fiew - Select iOS - Other and PCH File
(1) 添加新文件 - 选择 iOS - 其他和 PCH 文件
(2) add path of this PCH file to your Project - BuildSetting - Apple LLVM 6.0 Language
(2) 将此 PCH 文件的路径添加到您的项目 - BuildSetting - Apple LLVM 6.0 语言
Add Set Prefix Header Path YourApplicationName(root-path)/filename.pch
添加设置前缀头路径 YourApplicationName(root-path)/filename.pch