Syntax Clarification


#1

Are the following two statements the exact same thing?

  1. NSString * const WhereamiMapTypePrefKey = @“WhereamiMapTypePrefKey”;
  2. NSString const *WhereamiMapTypePrefKey = @“WhereamiMapTypePrefKey”;

Thanks in advance.


#2

To see what Xcode thinks, try this:

//  main.m

const NSString  *WhereamiMapTypePrefKey1 = @"WhereamiMapTypePrefKey";
NSString *const  WhereamiMapTypePrefKey2 = @"WhereamiMapTypePrefKey";
NSString  const *WhereamiMapTypePrefKey3 = @"WhereamiMapTypePrefKey";

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    // Mutate them
    WhereamiMapTypePrefKey1 = @"...";
    WhereamiMapTypePrefKey2 = @"...";
    WhereamiMapTypePrefKey3 = @"...";


    return 0;
}

#3

Using your code results in Xcode stating that the *const version is read-only and can’t be assigned.

So what does NSString const *WhereamiMapTypePrefKey mean then?

When I dropped in these iterations into the Whereami app, all 3 iterations did end up working. Two of the iterations generated a warning: “Sending ‘const NSString *__strong’ to parameter of type ‘NSString *’ discards qualifiers” but worked.

Thanks!