In class A I have this:
static NSString * const kMyConstant = @"my constant string";
How can I reference this from class B?
In class A I have this:
static NSString * const kMyConstant = @"my constant string";
How can I reference this from class B?
You need to remove the
static
-- that specifies thatkMyConstant
is only visible in files linked with this one.Then, declare (as opposed to defining) the string in Class A's header:
and import that header wherever you want to use this string. The
extern
declaration says that there exists anNSString * const
by the namekMyConstant
whose storage is created in some other place.If the static definition is already in the header, you need to move it elsewhere (usually the implementation file). Things can only be defined once, and if you try to import a file which defines a variable, you'll get a linker error.
You should extern your string in the header, and then define the string in the implementation.
If it's static, you can't (that's what the static keyword is for).
If you simply declare it as a global variable, however, you can do something like this: