Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Where to define macros to be available everywhere?

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.

like image 295
Tim Avatar asked Apr 24 '11 10:04

Tim


People also ask

What does #define do in Objective C?

In most cases. Many Objective-C apps use the #define preprocessor macro. These preprocessor macros tells the compiler to replace the definition of the macro before compilation. My team is using these macros in our Objective-C code to define constants.

What are macros Objective C?

Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.

Does Swift have macros?

Macros in Swift programming are generally the block of code available to the developer as a single statement. A single block of code consists of a large number of statements or instructions. Hence making the development less tedious.


2 Answers

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.

like image 145
ajay Avatar answered Sep 22 '22 10:09

ajay


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.

like image 31
Jhaliya - Praveen Sharma Avatar answered Sep 20 '22 10:09

Jhaliya - Praveen Sharma