You should create a header file like
(你应该创建一个像头文件)
// Constants.h
FOUNDATION_EXPORT NSString *const MyFirstConstant;
FOUNDATION_EXPORT NSString *const MySecondConstant;
//etc.
(you can use extern
instead of FOUNDATION_EXPORT
if your code will not be used in mixed C/C++ environments or on other platforms)
((如果您的代码不会在混合C / C ++环境或其他平台上使用,则可以使用extern
而不是FOUNDATION_EXPORT
))
You can include this file in each file that uses the constants or in the pre-compiled header for the project.
(您可以将此文件包含在使用常量的每个文件中,也可以包含在项目的预编译头中。)
You define these constants in a .m file like
(您可以在.m文件中定义这些常量)
// Constants.m
NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant";
Constants.m should be added to your application/framework's target so that it is linked in to the final product.
(应将Constants.m添加到应用程序/框架的目标中,以便将其链接到最终产品。)
The advantage of using string constants instead of #define
'd constants is that you can test for equality using pointer comparison ( stringInstance == MyFirstConstant
) which is much faster than string comparison ( [stringInstance isEqualToString:MyFirstConstant]
) (and easier to read, IMO).
(使用的优点字符串常量代替#define
倒是常数是可以测试用于使用指针比较平等( stringInstance == MyFirstConstant
)比字符串比较快得多( [stringInstance isEqualToString:MyFirstConstant]
和更容易阅读, IMO)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…