#define vs const and linking against frameworks

2019-08-29 12:28发布

问题:

I have a really small lightweight application which needs to use some constants that are stored in a larger framework. I don't want to duplicate these constants and hardcode them into the lightweight application but I also don't want to have to link against the large framework to just get the constants.

The constants are defined using static NSString *const in a header file. Does replacing the static NSString *const with #define prevent me from having to link against the whole framework ?

To be honest, I'm not entirely sure how linking works so I'm probably thinking about this incorrectly

回答1:

Yes, if you #define the constants you just need to #import the .h file that contains them.

You do need to be aware that the #defined constants are literal text substitutions -- they have no "type", etc, as static const values would.

But another option (for integer constants only) is to define C-style enums in a .h file.