0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JPC
  • 8,096
  • 22
  • 77
  • 110
  • 2
    If the framework switches to `#define` for these constants, it can no longer change them. As long as they're actual symbols, the framework author is free to change the values, since apps will pull the current value at runtime. But `#define` bakes the value in at compile-time. Whichever one you use really depends on how much you care about this. – Lily Ballard Nov 04 '11 at 23:56
  • That's ok, these values shouldn't change much – JPC Nov 05 '11 at 00:07
  • 1
    Why do you want to avoid linking against the framework? If you're worried about executable size, the linker will strip out any symbols from the framework that aren't referenced, so you only use what you need. – Adam Rosenfield Nov 05 '11 at 00:25

1 Answers1

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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Thanks, I read through a lot of posts here that said const is better so I wanted to make sure before I used #define. I also need strings so I can't use enums – JPC Nov 04 '11 at 23:56
  • 2
    Certainly #defines are considered a bit pedestrian by the programming elite, but really the C-based languages don't offer a lot of options, so you have to make do with what works. – Hot Licks Nov 05 '11 at 01:11