Reputation: 251
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
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
Reputation: 35783
This is not possible because this:
#define constant 3
...
y = x + constant
Is completely equivalent to this:
y = x + 3
#define
d 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