I'm trying to a NSString constant in my .h file to be defined in my .m. I understand that
extern NSString * const variableName;
in the .h and
NSString * const variableName = @"variableValue";
is the way to do this. Examining c tutorials I see that const
is supposed to go before variable definitions. My question is why is it not declared as extern const NSString * variableName;
in the .h and const NSString * variableName = @"variableValue";
in the .m. I know this doesn't work because I encounter compiler warnings which say 'Passing argument 1 of methodName: discards qualifiers from pointer target type'. What does this mean?
views:
83answers:
1
+6
A:
It's not the same. The const modifier can be applied to the value, or the pointer to the value.
int * const
A constant pointer (not modifiable) to an integer (its value can be modified)
const int *
A modifiable pointer to a constant integer (its value can't be modified)
So you can imagine:
const int * const;
Macmade
2010-07-07 15:57:39
To expand on that, if you're not dealing with a pointer, `const int` and `int const` are exactly the same. `NSString`s are already immutable, so the const only needs to be applied to the pointer (which can be a little confusing since Objective-C objects are always accessed via pointers).
Wevah
2010-07-07 16:34:26
thanks both of you, wevah thanks very much for the explanation of that, should have thought of it
dhatch387
2010-07-07 22:00:33