Today I used some of the usual strings in my project, so I created a constant.h file that was defined by # define.
Later on the internet to see the big gods are recommended to use string constants to define, so I have all the macro definitions are replaced with nsstring * const, such as nsstring * const ltnnextstep = @ " next ";
The. h file is used primarily in two classes, but a link problem was found after #import the file:
LD:one for1 (use-v to see invocation)
It turns out that the variable defined in standard C is an outer join, that is, if a compilation unit defines a global const constant, it is visible in other compilation units, and a duplicate definition error occurs if the other compilation unit also defines a const constant with the same name. This is different from C + +, where const-defined variables in C + + are internal, that is, the global const constant defined by each compilation unit is unique to itself.
Objective-c is another extension of standard C, then my mistakes are obvious-there is a duplicate definition error when multiple compilation units refer to that constant.h file.
But will
NSString * const, such as nsstring * const ltnnextstep = @ " next ";
Revision changed to
Static nsstring * const, such as nsstring * const ltnnextstep = @ " next ";
It was successfully compiled and passed. The static here is used to mark the defined const constants as invisible to the outside.
At present, my basic knowledge is not solid, so I can not say why. The study will be studied more carefully in the future.
Using a common method, create a. h and a. m file directly in the. h file:
@interface Constant:nsobject extern Const Ljnextstep; @end
Here the extern c keyword is used to indicate that the variable has been declared, just a reference. The Const keyword indicates that the variable is constant and cannot be modified.
In the. m file:
@implementationconst@ " next "; @end
Solutions for constants defined in OBJECTIVE-C