Objective-C: Where to define macros to be availabl

2020-06-18 03:48发布

问题:

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.

回答1:

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.



回答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.