6

I have an iOS application which uses in many view controllers, the same strings, numbers, etc. So I think it would be much better to define these constants in one file and use it in all the view controller implementations. The advantage will be changing one number, I have to do it only once and not in all view controllers this number is used.

Actually, I have in my .h file such macros:

#define aNumber 34.0 

and then I can use it in the corresponding .m file. But what's the best way to do this in the whole application? Creating a plain .h file which consists only of such macros and then importing this file in the .h files of the view controllers?

Best Regards, Tim.

2 Answers 2

12

The simple way is take constant.h and write all macro there.we have .pch class and import the constant.h into the .pch(pre compile header) file.No need to import constant.h file to each and every viewcontroller.

Sign up to request clarification or add additional context in comments.

5 Comments

I have a pch file in my project with some #ifdef and #ifndef which seems to be auto-generated. There I should place in #import "myConstants.h"?
#ifdef OBJC #import "Constants.h" #endif this is some sample example
Personally, I do not like it when people use the precompiled header for macro definitions. But that may be just me. I prefer manually including any header files that I need in my sources. That way it becomes easier to understand the structure and dependencies of my classes.
I think in general that's true @Till, however some macros I'd rather just have everywhere (like NSLog macros that automatically take logs out for non-debug builds)
how to do it in swift ?
2

I think, you are on right way to create a .h files and import that in .m file when needed,

Suggest you to place all the constant in One file and name that myApplicationConstants.h. and import it any number of .m files where the constant is needed.

3 Comments

Okay, and I only place #define variable1 test in this file, nothing else?!
@Tim - yes, is this a surprising result?
@Tim: You could place all the integer,string , font size , font name etc ..which is going to be shared across your application file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.