ProjectName-Prefix.pch is Missing in Xcode 6.
We can create .h file and use #define to create constants and then include this file to prefix file of our project before Xcode 6.
Sample constant.h file
#define OK #"OK"
Go to View Controller include file in header #import "Constants.h"
OR Define in pch file ,so that all View controllers can access the file
In viewDidLoad
NSLog(#"%#",OK);
My question is
where to include my constant.h file so can be access the file from all view controllers like we did using .pch file?
EDIT:
My question is replacement of .pch file ? As if apple remove it from Xcode their must be some alternate to achieve this goal without adding .pch and include in every single file to be used
You answered your question yourself. Here
We can create .h file and use #define to create constants and then include this file to prefix file of our project before Xcode 6.
You include the Common.h file inside the pch file. And you dont have to #import anything, as PCH file is available in all classes, and any import inside the PCH file is also available in all classes. Hence PCH files are termed as Pre-Compiled Header files.
You can configure a pch file in Build Settings->Prefix Header. Type in the path to the pch file or a .h file relative to the xcodeproj file.
Yes You can create header file for Xcode. I am sure pch is best way to used whole application. You can import constant file on pch and used in whole application.
Related
After I updated my Xcode toXcode 8.
I found out that if I import vendor framework header files in .pch file. there will be a error. 'xxx' .h file not Found
Such as:
I want to import a framework named Umeng's header file.
So I write the codes below to my XXX-prefix.pch
#import <UMMobClick/MobClick.h>
But Error UMMobClick/MobClick.h file not found appeared.
Any constriction of Xcode that prohibit importing vendor framework header files in .pch file?
I had a similar problem with a legacy Xcode project that used a .pch file -- the file was being ignored.
The fix was in build settings, where the original path to this file assumed it began at the project root:
SharkAlert/SharkAlert-Prefix.pch
Xcode 8 apparently requires an explicit reference to the starting point
$(SRCROOT)/SharkAlert/SharkAlert-Prefix.pch
Now the project builds as expected.
If this file you are used to bridge OC and Swift,now you should creat a new .header file and Move All Header file Name inside file
I had a similar issue. Changing #import <Framework/Framework.h> to #import Framework; fixed it for me.
I have recently added a Swift file to my Objective-C only project. Xcode created the necessary header files, including the Objective-C Generated Interface Header. Everything works fine if I #import this header file "myProject-Swift.h" to the .m file where I am accessing the Swift class I have added.
However I will want to eventually use this Swift class throughout my project, and to avoid importing it every single time, I wanted to add this myProject-Swift.h import file to my .pch prefix file.
However when I try to build my app after having added this, I get a 'myProject-Swift.h' file not found error and the build fails.
Does this mean it is not possible to import this myProject-Swift.h in a .pch file?
Is it really only possible to do it in each .m file individually?
I am getting an error that it can not find the header file, but I have added the file via "link binary with library" under Build Phases.
The file is WhirlyGlobeComponent.h
and I imported... #import "WhirlyGlobeComponent.h" and it said that the file can not be found.
What could I doing wrong. I have already cleaned the project.
Just drag and drop the .h and the .m file into your swift project. Then Xcode should create a Briding-Header.h file for you. In there you have to add the .h file:
#import "WhirlyGlobeComponent.h"
After that, you should be able to use the WhirlyGlobeComponent without any additional imports in your swift files.
Why isn't ProjectName-Prefix.pch created automatically in Xcode 6 ?
Is the precompile header no longer needed ?
Where should I write the code that was in ProjectName-Prefix.pch before ?
Without the question if it is proper or not, you can add PCH file manually:
Add new PCH file to the project: New file > Other > PCH file.
At the Target's 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).
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 Settings option, set the value of Precompile Prefix Header to YES.
I suspect because of modules, which remove the need for the #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.
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.
In that case you can create one and pass it into clang, but it's incredibly rare that it's a good idea.
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 especially important for categories, which can make code very hard to reuse if they're implicit.
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).
You need to create own PCH file
Add New file -> Other-> PCH file
Then add the path of this PCH file to your build setting->prefix header->path
($(SRCROOT)/filename.pch)
I'll show you with a pic!
Add a new File
Go to Project/Build Setting/APPl LLVM 6.0-Language
To add .pch file-
1) Add new .pch file to your project->New file->other->PCH file
2) Goto your project's build setting.
3) Search "prefix header". You can find that under Apple LLVM.
4) Paste this in the field $(SRCROOT)/yourPrefixHeaderFileName.pch
5) Clean and build the project.
That's it!!!
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:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
Use :
$(PROJECT_DIR)/Project name/PrefixHeader.pch
For add new PCH file follow bellow steps :
(1) Add New fiew - Select iOS - Other and PCH File
(2) add path of this PCH file to your Project - BuildSetting - Apple LLVM 6.0 Language
Add Set Prefix Header Path YourApplicationName(root-path)/filename.pch
So many developers are adding various convenience macros to the Prefix.pch. But my question is what is that Prefix.pch file.
If i remove that Prefix.pch file from my Xcode, then will my application run? Or will it show any error? Or will it crash during build?
How can i run my application without Prefix.pch file
Precompiled header.
What is it?
A Prefix.pch is a precompiled header. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.
Xcode
In Xcode, you add imports of the header files you want in a “prefix header,” and enabling Precompile Prefix Header so they get precompiled. But the idea behind a prefix header is different from precompiling.
A prefix header is implicitly included at the start of every source file. It’s like each source file adds
#import "Prefix.pch"
at the top of the file, before anything else.
Removing it.
You can remove the precompiled header. This question has been already answered in thread I'm linking below. It contains all the information you need as well as useful comments.
Is it OK to remove Prefix.pch file from the Xcode project?
What is Prefix.pch file?
A .pch is a Pre-Compiled Header.
In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor, usually specified by the use of compiler directives in the source file.
Prefix headers are compiled and stored in a cache, and then automatically included in every file during compilation. This can speed up compilation, and lets you include a file without adding an import statement to every file using it. They are not required, and actually slow compilation whenever you change them.
Yes, you can compile and run the project without .pch file
In Xcode, go to your target's build settings (Command-Option-E, build tab) and uncheck Precompile Prefix Header (GCC_PRECOMPILE_PREFIX_HEADER). You can also remove the value of the Prefix Header setting if you wish.
Also note that,
Don't put macros in a.pch file! A .pch file is, by definition, a project specific precompiled header. It really shouldn't be used beyond the context of the project and it really shouldn't contain anything but #includes and #imports.
If you have some macros and such that you want to share between headers, then stick 'em in a header file of their own — Common.h or whatever — and #include that at the beginning of the .pch
Prefix headers are compiled and stored in a cache, and then automatically included in every file during compilation. This can speed up compilation, and lets you include a file without adding an import statement to every file using it. They are not required, and actually slow compilation whenever you change them.
Generally .pch file naming formation is yourProjectName-Prefix.pch
Prefix Headers and Precompiled Headers (.pch)
History:
[#include -> #import] -> Precompiled Headers .pch -> [#import Module(ObjC);] -> [import Module(Swift)]
Precompiled Headers - prefix.pch
To create new .pch file
File -> New -> File... -> PCH File
PrefixHeader.pch sample:
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
From Xcode 6 you should set .pch manually. Previously it was called <product_name>-Prefix.pch
Xcode:
Prefix Header(GCC_PREFIX_HEADER) - path to .pch. Adds all content from .pch to all source files
SomeFile1.h
//You can skip this section. It will be compiled
//#import <UIKit/UIKit.h>
//#import <Foundation/Foundation.h>
//implicitly next line is used
#import "PrefixHeader.pch"
Precompile Prefix Header(GCC_PRECOMPILE_PREFIX_HEADER) - When Yes then Prefix headers from GCC_PREFIX_HEADER will be used once* to generate precompiled sources and will be stored in a cache which speeds up a build time
//path to cached precompiled headers
/<some_path>/DerivedData/ExperiementsModuleSwift-dsetoksxykdmgvczgtnmaqmjwhzk/Build/Intermediates.noindex/PrecompiledHeaders/SharedPrecompiledHeaders/14385052922615550324
//e.g
/Users/alex/Library/Developer/Xcode/DerivedData/ExperiementsModuleSwift-dsetoksxykdmgvczgtnmaqmjwhzk/Build/Intermediates.noindex/PrecompiledHeaders/SharedPrecompiledHeaders/14385052922615550324
#import vs .pch
This feature improves build time(comparing with #include/#import) because precompiles a prefix headers(global). When some content of prefix headers is changed it means that Compiles must recompile that again. that is why you should use stable(not often changeable) prefix headers here or you dont't see build time improvement. Also you should support it