iOS Prefix.Pch Best Practices

iOS Prefix.pch best practices

Ewww… 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.

Best practices for adding compile sources to a Prefix.pch file

One option might be to put your categories together into a library, and then refer to that library in your current project. See Ray Wenderlich's example at:

http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial

What is Prefix.pch file in Xcode?


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?

Iphone write code in prefix.pch

Pre-compiled header files were brought to serve one purpose: to make compiling faster. It is compiled and stored in cache, and automatically included in every source file during the compilation time. Its like each source file does,

#import "Prefix.h"

This can be handy for project-wide #defines. (FYI, #defines are a code smell)

Xcode quotes: Precompiling the prefix header will be most effective if the contents of the prefix header or any file it includes change rarely. If the contents of the prefix header or any file it includes change frequently, there may be a negative impact to overall build time.

More clear explanation is here

Please keep this in mind when you #import s source file header in .pch. I would suggest you to explore other ways to write your code rather than choosing .pch file.

You can use Prefix.h for #import of constants and utility source files. Also for convenience in debugging like this:

#ifndef DEBUG
#define NSLog(x,...)
#endif

I see that you want to declare constant strings to use project-wide. Create a new header file "Constants.h"(or "Global.h", as you like it) and write all your global constants(usually macros & typedef enum's) here. However, to declare constant strings using extern you would need implementation file too.

In "Constants.h",

extern NSString *const app_ID;

And in "Constants.m",

NSString *const app_ID=@"dfgdf";

Hope that helps.

Should I put all header file into Prefix.pch?

I add all the headers to Prefix.pch, and I have to claim life is much easier since that time. I just do not have to import always headers, believe me, life is much easier. :-)

Prefix.pch: how to not use it for certain files?

I'd suggested not to use @import. Use instead old fashion #import.
If you have difficulties with that, then use @import's only exactly where you need them, like in Swift.

There is also one possible solution.
Put yours import in #ifdef __OBJC__ #ifndef __cplusplus #endif #endif block.
Example

#ifdef __OBJC__
#ifndef __cplusplus
@import ...
#endif
#endif

How to put macros and constants into .pch file in an iOS project?

Precompiled headers are meant to improve overall compile times for projects, so the design of precompiled headers is entirely driven by performance concerns.
The use case for precompiled headers is relatively simple: when there is a common set of headers that is included in nearly every source file in the project, we precompile that bundle of headers into a single precompiled header (PCH file).

The .pch file allows you to import files like UIKit.h and Foundation.h(rather than importing them in every .h of your project) If you have those files imported in the .pch, in your own classes don't need to import them.

The significance of #ifdef OBJC is so that you don't import headers containing objective-c code if you don't have the compiler set to build objective c code (hence avoiding lots of compiler errors).

How do I safely change things like my Prefix.pch file location, sqlitelite file name, etc. after changing app name?

You can change the path to your pch file in the build settings for your target. The build setting is named Prefix Header and should be a relative path to the pch file from the root of your project.

As for the SQLite file that CoreData uses, unless you're talking about a file that's included in your application resources, you should just be able to change the file name you point your CoreData stack to while you're creating it. Note that this will essentially have the affect of making your app create a new database file the next time it launches, so you'll have to do something more complex if you've already submitted the app and have users using the current name.

how to write condition in prefix.pch

You can compare height with value in points for 4 inch display like this:

#define iPhone4Or5 [[UIScreen mainScreen] bounds].size.height == 568 ? 5 : 4


Related Topics



Leave a reply



Submit