Objective-C static constant variables

I am trying to create a class with static constant variables that can be used from outside the class, but I cannot figure out how to initialize this variable.

Code example:

@interface ExampleClass { static const int CONST_VAR; } - (id) init; @end @implementation ExampleClass - (id) init { CONST_VAR = 1; } @end 

I want to be able to refer to a static constant variable as follows:

 ExampleClass.CONST_VAR; 
+4
source share
1 answer

You must assign a value to this static variable by doing the following:

 -(id)init{ ExampleClass.CONST_VAR = 1; } 

Since this is a static variable or a "class variable", you should use the class name anyway, no matter where you are, from within the same class.

Hope this helps.

+1
source

All Articles