在 Xcode 6 / iOS 8 中全局导入文件的方式是否发生了变化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27034161/
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-15 06:13:08  来源:igfitidea点击:

Did the way how to globally import files change in Xcode 6 / iOS 8?

iosobjective-cxcodeios8xcode6

提问by nburk

I used to do my global imports (i.e. imports that would be visible to allsource files in my Xcode project) in the file AppName-prefix.pch.

我曾经在文件AppName-prefix.pch 中进行全局导入(即对我的 Xcode 项目中的所有源文件可见的导入)。

However, now in an Xcode 6 (and iOS 8) environment, after having created a new project, this file is not automatically generated any more.

但是,现在在 Xcode 6(和 iOS 8)环境中,创建新项目后,不再自动生成此文件。

My question is how to properly do global imports in Xcode 6? Can I just create my own AppName-prefix.pchand use this one eventually?

我的问题是如何在 Xcode 6 中正确地进行全局导入?我可以创建自己的AppName-prefix.pch并最终使用这个吗?

Note:When using Cocoapods, a file called Pods-AppName-Bolts-prefix.pchis generated. But global imports won't be working with this one.

注意:使用Cocoapods 时,会生成一个名为Pods- AppName-Bolts-prefix.pch的文件。但是全球进口不会与这个合作。

回答by l0gg3r

Apple finally understood that having global dependency is a Very Very bad practice. Ideally you need to stop using PCH files, because it makes other files very messy, and breaks code reusing.

苹果终于明白,拥有全局依赖是一种非常非常糟糕的做法。理想情况下,您需要停止使用 PCH 文件,因为它会使其他文件变得非常混乱,并且会破坏代码重用。

Anyway, here is solution

无论如何,这是解决方案

  1. Add new PCH file to the project - New file > Other > PCH file

  2. At the project 'Build Settings' option - set the value of 'Prefix Header' to your PCH file name, with the project name as prefix - i.e. for project named 'TestProject' and PCH file named 'MyPrefixHeaderFile', add the value 'TestProject/MyPrefixHeaderFile.pch' to the plist.

  1. 向项目添加新的 PCH 文件 - 新建文件 > 其他 > PCH 文件

  2. 在项目“构建设置”选项中 - 将“前缀标题”的值设置为您的 PCH 文件名,以项目名称作为前缀 - 即对于名为“TestProject”的项目和名为“MyPrefixHeaderFile”的 PCH 文件,添加值“TestProject” /MyPrefixHeaderFile.pch' 到 plist。

回答by Wain

You can creTe your own, but the point really is that you shouldn't have global dependencies, you should explicitly import only what is required into each class. Indeed, in your .h files you should endeavour to use @classfor everything and only import into your .m file. This approach makes the dependencies of a class very clear and reduces the likelihood of dependency circularities, which are both very good things.

你可以创建你自己的,但关键是你不应该有全局依赖,你应该只将需要的内容显式导入每个类。实际上,在您的 .h 文件中,您应该尽量使用@class所有内容,并且只导入到您的 .m 文件中。这种方式使得一个类的依赖关系非常清晰,并且减少了依赖循环的可能性,这都是非常好的事情。

回答by Rameswar Prasad

In Xcode 6 the project's .pch file not anymore generated automatically but if you want one then you can create a .pch file yourself. Do this as below;

在 Xcode 6 中,项目的 .pch 文件不再自动生成,但如果你想要一个,那么你可以自己创建一个 .pch 文件。这样做如下;

Goto->File->New->File->(Under iOS)Other->PCH File->Next

After entering the name for the file and saving it, go to "Build Settings" and set the insert the value for "prefix header" as "YourProjectName/YourPCHFileName.pch". And if the "Precompile Prefix header" is set to "No" then change it to "Yes". Now you can put any file you want to import globally, but keeping everything here is not a good practice.

输入文件名称并保存后,转到“Build Settings”并将“prefix header”的插入值设置为“YourProjectName/YourPCHFileName.pch”。如果“预编译前缀头”设置为“否”,则将其更改为“是”。现在,您可以将要导入的任何文件放在全局范围内,但将所有内容保留在此处并不是一个好习惯。