NSString* const nits = @"nits";
NSString* const nuts = nits; // error: "initializer element is not constant"
How is this done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Constant String literals such as your
@"nits"are hardcoded into objective-c binary files. String constants such asnitsandnutshave to be defined as a constant string literal, as they are hard-coded as well. The assignmentnuts = nitsdoes not work becausenitsis not a constant string literal, even though the variable is constant at runtime.There are two ways to fix this:
@"nits"twice#defineThis might look like this:
Alternatively you can use
#defines only, but these cannot be exported in a header as cleanly as const strings.