Defining a constant and passing it as a parameter leads to "discards the qualifier from the target pointer type"

This has been asked so many times in the forum, but I need to clarify the concept behind it. I declare const NSString * key = @ "SomeConstValue"; and pass this to a method that takes an NSString pointer type. However, this leads me to some warning โ€œdiscards the qualifier from the target pointer typeโ€. What does it mean? How can I create a constant and pass it to this method?

+4
source share
1 answer

In the case you described, you drop the "const" qualifier (pass const NSString * to a method that expects NSString *).

NSStrings are always immutable, so you can safely declare a constant as "NSString *". If you want to trigger compiler warnings when assigning values โ€‹โ€‹to a variable, declare it as "NSString * const".

+5
source

All Articles