What is the significance of the positioning of the
const
keyword when declaring a variable in Objective-C, for example:
extern const NSString * MY_CONSTANT;
versus
extern NSString * const MY_CONSTANT;
Using the first version in assignments produces warnings about "qualifiers from pointer target type" being discarded so I'm assuming that the second version ensures that the pointer address remains the same. I would really appreciate a more definitive answer though. Many thanks in advance!
In the first case, you are declaring a mutable pointer to an immutable const NSString
object, whereas in the second case, you are declaring an immutable pointer to a mutable NSString
object.
An easy way to remember this is to look at where the *
is situated; everything to the left of it is the "pointee" type, and everything to the right of it describes the properties of the pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With