Constants in Objective-C

定义了一些常量,引入之后还是报找不到的错误,最后是因为没有放到预编译文件里面。

Constants.hFOUNDATION_EXPORTNSString*constMyFirstConstant;FOUNDATION_EXPORTNSString*constMySecondConstant;//etc.

(you can useexterninstead ofFOUNDATION_EXPORTif your code will not be used in mixed C/C++ environments or on other platforms)

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

// Constants.mNSString*constMyFirstConstant=@"FirstConstant";NSString*constMySecondConstant=@"SecondConstant";

Constants.m should be added to your application/framework's target so that it is linked in to the final product.

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).

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容