NSString* const nits = @"nits";
NSString* const nuts = nits; // error: "initializer element is not constant"
How is this done?
NSString* const nits = @"nits";
NSString* const nuts = nits; // error: "initializer element is not constant"
How is this done?
I don't know objective-c but I would imagine that by initialising something from a value that is not a literal then you end up with not a constant - regardless of the initial value declaration.
Constant String literals such as your @"nits"
are hardcoded into objective-c binary files. String constants such as nits
and nuts
have to be defined as a constant string literal, as they are hard-coded as well. The assignment nuts = nits
does not work because nits
is not a constant string literal, even though the variable is constant at runtime.
There are two ways to fix this:
@"nits"
twice#define
This might look like this:
#define NitsNutsString @"nits"
NSString* const nits = NitsNutsString;
NSString* const nuts = NitsNutsString;
Alternatively you can use #define
s only, but these cannot be exported in a header as cleanly as const strings.