87

I'm using NSUSerDefaults to store user preferences. I remember reading somewhere that setting the keys as constants is a good idea - and I agree. The following code is what I currently have:

[[NSUserDefaults standardUserDefaults]
        setObject:[NSNumber numberWithInt:polygon.numberOfSides] 
           forKey:@"polygonNumberOfSides"];

I tried changing this to:

@implementation Controller

NSString const *kPolygonNumberOfSides = @"polygonNumberOfSides";

-(void)savePolygonInfo {
    [[NSUserDefaults standardUserDefaults]
            setObject:[NSNumber numberWithInt:polygon.numberOfSides] 
               forKey:kPolygonNumberOfSides];
}

While this does work, it produces "warning: passing argument 1 of 'objectForKey:' discards qualifiers from pointer target type". I'm keen to keep my code free from compiler warnings. How can I fix this warning?

e.James
  • 116,942
  • 41
  • 177
  • 214
Olly
  • 3,409
  • 3
  • 24
  • 28

4 Answers4

212

You should use:

NSString * const kPolygonNumberOfSides = @"..."; // const pointer

instead of:

NSString const * kPolygonNumberOfSides = @"..."; // pointer to const

The first is a constant pointer to an NSString object, while the second is a pointer to a constant NSString object.

It is a subtle difference. The compiler warning happens because setObject:forKey: is declared as follows:

- (void)setObject:(id)value forKey:(NSString *)defaultName;

It is expecting the defaultName argument to be of type NSString *. When you instead pass in a pointer to a constant, you've given it something different.

Update: I want to point out that these constants should be defined as static if they are only going to be used from within a single file. I say this because I have run across this problem myself: if you do not declare them as static, then they will exist in the global namespace, and you will not be able to use a variable with the same the same name in another file. see Constants in Objective-C for more information. To explain by example, this is what I currently use for keys that I only need to use in one .m file:

static NSString * const kSomeLabel = @"...";
Community
  • 1
  • 1
e.James
  • 116,942
  • 41
  • 177
  • 214
  • 1
    `NSString * const foo` works because `NSString` is immutable and the pointer is immutable so it can never change correct? Also, I recall from C++ that `const` is implicitly `static` (a compiler optimization) so no need to call it out. Is that true here as well? – Ternary Dec 21 '15 at 17:02
33

Don't use const with Objective-C objects, they weren't really designed to use it. NSString objects (among many others) are already immutable by default by virtue of their design, so making them const is useless.

As e.James suggested, you can use an NSString * const, which is a constant pointer to an NSString. This is subtly different from a const NSString * (equivalent to NSString const *), which is a pointer to a constant NSString. Using a NSString * const prevents you from reassigning kPoly to point to a new NSString object.

Community
  • 1
  • 1
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • Good point about the use of const. That's why a lot of Objective-C classes have "Mutable" variants. – e.James Apr 15 '09 at 21:45
  • 3
    I thought that `const` also means you can't reassign it. I guess I had that wrong. – Dan Rosenstark Feb 07 '11 at 00:42
  • Given your 2nd paragraph, I think you should amend your first paragraph. Not being able to reassign an identifier is a valid use, and doesn't really have anything to do with mutability of the object itself. If you declare an NSString as ```NSString* const foo = @"bar"``` and then later try to reassign it, you'll get the error ```Cannot assign to variable 'foo'...``` which is exactly what I'd hope for when declaring something const. – vegashacker Apr 14 '21 at 19:11
18

For access from other classes:

.h

extern NSString * const PolygonNumberOfSidesPrefsKey;

.m

NSString * const PolygonNumberOfSidesPrefsKey = @"PolygonNumberOfSides"

For access only inside current class:

.m

static NSString * const kPolygonNumberOfSidesPrefsKey = @"PolygonNumberOfSides"
malhal
  • 26,330
  • 7
  • 115
  • 133
5

I would suggest even making the constant more descriptive. A constant for the number of sides of a polygon could come from anywhere. As a suggestion, how about:

kDefaultsPolygonNumberOfSides;

instead.

Abizern
  • 146,289
  • 39
  • 203
  • 257