8

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

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

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

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

nburk
  • 22,409
  • 18
  • 87
  • 132
  • Yup, its been discouraged to use pch from latest inclusion, because it increases build time, and create problems, good practice is to import classes where it is necessary, but its painful if you have lots of classes and want a global import, you can either chose to do it manually by creating a `pch` file. – iphonic Nov 20 '14 at 07:47

3 Answers3

20

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.

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.

l0gg3r
  • 8,864
  • 3
  • 26
  • 46
  • I'll go with finally importing my logger in every class for its own, you guys convinced me! :) Plus, Apple usually knows what they're doing when inducing such a change, so I guess for the sake of being a good citizen I'll dismiss my global import plan... Thanks for the clarification anyway! :) – nburk Nov 20 '14 at 07:52
  • Thank you for your answer. But I don't understand why we shouldn't use global import. Can you explain why or give some instructions? – sahara108 Nov 20 '14 at 08:45
  • 2
    @sahara108 first off all it's decreasing compiling speed, also It's a bad practice because classes can't be ported to another project (because they are using global methods/macros/functions) – l0gg3r Nov 20 '14 at 09:59
  • It is quite interesting. When I first started out in Xcode, I just imported header file where it is needed. But then I read a lot of comments on SO to do it in PCH file. Some of the issues I have encountered related to it. – user523234 Nov 28 '14 at 15:20
  • 1
    What about the use of Macros? Where would you place them now? I've always done in the PCH file. – Paul Jan 18 '15 at 04:37
  • @Paul i'm creating Macro.h, and importing it, where i use them. – l0gg3r Jan 18 '15 at 08:54
5

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 @class for 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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I fully agree that global imports are not good practice. I just want to use it for logging to make sure my custom Logger is imported in all my classes. Are there better ways to solve this? – nburk Nov 20 '14 at 07:45
  • @nburk you need to import your Logger everywhere, or use inheritance to make global things combined. – l0gg3r Nov 20 '14 at 07:47
0

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;

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.

Rameswar Prasad
  • 1,331
  • 17
  • 35
  • @l0gg3r Sorry while I was typing my answer you already posted so didn't see yours. And yes I checked in my case, you're correct. I need to follow the 2nd step which you provided. Thanks for the suggestion also. – Rameswar Prasad Nov 20 '14 at 07:48