Adapted from https://stackoverflow.com/a/3129608/
What you can do is put in your header ( MyConstants.h ):
extern const int MyConstant; extern NSString * const MyStringConstant;
And in the source file, include the header above, but define the constants ( MyConstants.m ):
const int MyConstant = 123; NSString * const MyStringConstant = @"SomeString";
Then you just need to include the header in any other source file that uses any of these constants. The header simply declares that these constants exist somewhere, so the compiler will not complain, because it is the job of the linker to resolve these constant names. The source file containing your constant definitions is compiled, and the linker sees that these are constants, and resolves all the links found in other source files.
The problem with declaring and defining a constant in the header (which is not declared as static ) is that the compiler treats it as an independent global for every file that includes this header. When the linker tries to link all compiled sources together, it encounters the global name as many times as you included MyConstants.h .
CodeReaper 05 Oct '16 at 2:58 p.m. 2016-10-05 14:58
source share