I have a defined a custom error domain that I want to publish in a protocol. The domain is defined in the implementation file like this:
//in the .m file
static void *MyErrorDomain = (void *)@"MyErrorDomain";
The .h file implements a protocol, and I would like to publish MyErrorDomain there as well. I can’t, however, figure out the correct form. The one that gives the fewest errors is:
// in the protocol
static extern void * TBPluginErrorDomain;
The Xcode error is: “Multiple storage classes in declaration specifiers”.
I thought that the static void construction turns MyErrorDomain into a kind of function, but “static extern void TBPluginErrorDomain();” only incremented the number of errors. In short, I’m wandering in a morass of ignorance and all guidance will be greatly appreciated.
At the top-level,
staticmeans “not directly accessible (without a pointer) outside this file” (file scope).externmeans “defined in some other object file”.static externdoes not really make any sense. It is like asking for access to a private member from another class. You do not want to usestatichere.Just make a normal definition in your
.m:And make an extern declaration in your
.h:The be sure to link in the
.ofrom the.mwhenever you link together something that uses the variable (after including the.hfile). In Xcode, in the Targets tab of the Get Info window for the.mfile, be sure to check each target that uses the variable (maybe slightly different in newer versions, mine is old!).You can probably add in
const(const …andextern const …), but NSString instances are immutable, so it is not totally necessary. You might make it into aconstpointer though, so the pointer can not be directly changed at runtime (without casting away theconstness). Together you haveconst void * const MyErrorDomainin the definition. Just prefix it withexternin the declaration.Also, depending on your purposes, you might consider using a
NSString *instead ofvoid *.