Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers" warning

I have Constants NSString, that I want to call like:

[newString isEqualToString:CONSTANT_STRING]; 

Any wrong code here?

I got this warning:

sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers

How should these be declared?

like image 907
user4951 Avatar asked Jul 26 '11 10:07

user4951


1 Answers

You should declare your constant string as follows:

NSString * const kSomeConstantString = @""; // constant pointer 

instead of:

const NSString * kSomeConstantString = @""; // pointer to constant // equivalent to NSString const * kSomeConstantString = @""; 

The former is a constant pointer to an NSString object, while the latter is a pointer to a constant NSString object.

Using a NSString * const prevents you from reassigning kSomeConstantString to point to a different NSString object.

The method isEqualToString: expects an argument of type NSString *. If you pass a pointer to a constant string (const NSString *), you are passing something different than it expects.

Besides, NSString objects are already immutable, so making them const NSString is meaningless.

like image 163
albertamg Avatar answered Sep 20 '22 17:09

albertamg