1

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.

2
  • It would have to be a non-constant global variable. Commented Feb 7, 2015 at 2:19
  • you can define a property and set its default value on viewWillAppear or viewDidLoad and change it later in the future.. Commented Feb 7, 2015 at 2:22

2 Answers 2

5

#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;
Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

As long as enabling to change a constant value within - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, I would be happy. So I am going for "y = x + 3" program. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.