Objective-C中的常量和“重复符号”链接器错误
我已经用.m
文件中的一些不同的类声明了一个常量,如下所示:
@implementation MyViewController const NSInteger numberOfItems = 6; ... @end
但是在尝试构build项目时,出现“重复符号”错误。 我发现了几个处理这个问题的外部或全局常量,但我想要的只是声明一些常量私有的类,我怎么能做到这一点?
谢谢
如果只想在一个.m
文件中使用常量,则将其声明为static
。 例如: static NSString * const CONSTANT_STRING = @"Constant I am"
。
在NSInteger
情况下,您可以在每个.m
文件中写入:
static const NSInteger my_const = 3;
如果你想要全局variables(一个常量,每个文件中都有一个可见值),那么写入你的.h
:
extern const NSInteger my_global_const;
并在你的.m
文件中添加
const NSInteger my_global_const = 5;