Objective-C中的常量和“重复符号”链接器错误 [英] Constants in Objective-C and "duplicate symbol" linker error

查看:184
本文介绍了Objective-C中的常量和“重复符号”链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些不同的类中声明了一个不变的名称,在他们的 .m 文件中,这样:

I've declared a constant with the same name in some different classes, in their .m file, this way:

@implementation MyViewController
const NSInteger numberOfItems = 6;
...
@end

但是我得到一个重复的符号尝试构建项目时出错。我发现有几个关于外部或全局常量问题的帖子,但是我想要的只是在他们的课堂上公开一些常量,我该怎么做?

But I get a "duplicate symbol" error when trying to build the project. I've found several posts dealing with this issue regarding extern or global constants, but what I'd want is just declaring some constants private to their class, how can I do that?

谢谢

推荐答案

如果您只想在一个 .m file然后将其声明为 static
例如: static NSString * const CONSTANT_STRING = @Constant I am

If you want to use constant only in one .m file then declare it as static. For example:static NSString * const CONSTANT_STRING = @"Constant I am".

如果 NSInteger 您可以写入您的每个 .m 文件:

In case of NSInteger you can write in your every .m file:

static const NSInteger my_const = 3;

如果您想要全局变量(一个常量,每个文件中可以看到一个值),然后写入 .h

If you want globals (one constant with one value visible in every file) then write in your .h:

extern const NSInteger my_global_const;

在您的 .m 文件中添加

const NSInteger my_global_const = 5;

这篇关于Objective-C中的常量和“重复符号”链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆