<AppName>.pch file usage
Asked Answered
C

2

6

What is the importance of .pch file and what is the significance of"#ifdef OBJC"?

Also, where do we define parameters like "#ifdef IS_PRODUCTION" which are checked in .pch file.

Cheshire answered 17/2, 2011 at 19:35 Comment(0)
C
13

.pch is a Pre-Compile 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.

#ifdef OBJC lets the compiler know that the code is Objective-C.

#ifdef IS_PRODUCTION is something you have defined on your own, a directive telling the compiler to do something only if this is defined, most-likely something for a PRODUCTION build.

Conker answered 17/2, 2011 at 19:37 Comment(3)
Ok. What is the significance of "#ifdef OBJC"? and also where do we define the macros we put in the if condition like "#ifdef IS_PRODUCTION"?Cheshire
You can define these anywhere, to use them, just import the file they are in.Conker
A bit anal retentive, maybe, but "#ifdef OBJC lets the compiler know that the code is Objective-C" is quite misleading as it is not "telling" anybody anything, but rather simply checking if ObjC is known/available at compile time and ensuring that its contained code is otherwise omitted during compilation. The effect might be the same in 9 out of 10 cases, yet the technical implications are significantly different. The compiler does not gain any more information via #ifdef OBJC, rather quite the opposite. It leads to omission of (and potentially critical) code after all.Marionmarionette
P
15

The .pch file allows you to avoid importing common files like UIKit.h and Foundation.h. If you have those files imported in the .pch, 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).

You define parameters such as IS_PRODUCTION inside the target's build settings. I do it usually in "other C flags".

Predestine answered 17/2, 2011 at 19:42 Comment(0)
C
13

.pch is a Pre-Compile 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.

#ifdef OBJC lets the compiler know that the code is Objective-C.

#ifdef IS_PRODUCTION is something you have defined on your own, a directive telling the compiler to do something only if this is defined, most-likely something for a PRODUCTION build.

Conker answered 17/2, 2011 at 19:37 Comment(3)
Ok. What is the significance of "#ifdef OBJC"? and also where do we define the macros we put in the if condition like "#ifdef IS_PRODUCTION"?Cheshire
You can define these anywhere, to use them, just import the file they are in.Conker
A bit anal retentive, maybe, but "#ifdef OBJC lets the compiler know that the code is Objective-C" is quite misleading as it is not "telling" anybody anything, but rather simply checking if ObjC is known/available at compile time and ensuring that its contained code is otherwise omitted during compilation. The effect might be the same in 9 out of 10 cases, yet the technical implications are significantly different. The compiler does not gain any more information via #ifdef OBJC, rather quite the opposite. It leads to omission of (and potentially critical) code after all.Marionmarionette

© 2022 - 2024 — McMap. All rights reserved.