user1574429
user1574429

Reputation: 251

How to change #define value

I know #define must be constant but please give me any good tips.

In my case, I define a constant value by #define (e.g. #define kImageQuality 0.7). However, I would like to change the constant value from Settings.Bundle before opening an app. That means to change the constant value, doesn't it? Is that any way to implement as my aim?

It should change to instance variable instead of #define? Any tips given by you that would be really appreciated.

Upvotes: 1

Views: 6933

Answers (2)

carloabelli
carloabelli

Reputation: 4349

#define constants are replaced before compilation even begins by the preprocessor (e.g. kImageQuality gets replaced by 0.7 before compilation). Therefore loading it before the app starts is impossible as the app does not recompile every time. You need to use a variable:

float imageQuality = 0.7f;

Upvotes: 5

Linuxios
Linuxios

Reputation: 35783

This is not possible because this:

#define constant 3
...
y = x + constant

Is completely equivalent to this:

y = x + 3

#defined constants are replaced by their value in the preprocessing stage before the code is even compiled. To change the value dynamically, you have to either use a global variable, or some other persistent mechanism like NSUserDefaults.

Upvotes: 4

Related Questions