Skip to content

Instantly share code, notes, and snippets.

@pyanfield
Created June 17, 2014 03:58
Show Gist options
  • Save pyanfield/f7b29489eb8c9c9f4092 to your computer and use it in GitHub Desktop.
Save pyanfield/f7b29489eb8c9c9f4092 to your computer and use it in GitHub Desktop.
Constants in Objective-C

定义一个全局的常量以便在 Objective-C 的项目中使用。可以通过创建一个头文件,比如 Constants.h,然后在头文件声明如下:

FOUNDATION_EXPORT NSString *const MyFirstConstant;
FOUNDATION_EXPORT NSString *const MySecondConstant;

然后在你的执行文件中 Constants.m 中:

NSString *const MyFirstConstant = @"FirstConstant";
NSString *const MySecondConstant = @"SecondConstant";

将你的 Constants.m 文件添加到你的 application/frameworktarget 中。这样你就可以使用这些全局的常量了。

注意这里使用的 FOUNDATION_EXPORT 因为,当你在 objective-c 中引入了 Foundation Framework 之后,你最好是用 FOUNDATION_EXPORT 来代替 extern, 因为在 NSObjCRuntime.h 中它包括了一些 C 和 C++ 的库,为了能更好的和这些 C 和 C++ 库兼容,所以建议用 FOUNDATION_EXPORT

http://stackoverflow.com/questions/538996/constants-in-objective-c http://stackoverflow.com/questions/19192432/when-to-use-foundation-export?lq=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment