如何使用一个NSArray作为一个全局常量? [英] How can I use an NSArray as a global constant?
问题描述
我用一组 Constant.m
文件,每个目标之一,确定具体的事情对每个目标。例如:
I'm using a set of Constant.m
files, one per target, to define specific things for each target. For example:
// Constants.h
extern NSString * const kDatabaseFileName;
//Constants.m
NSString * const kDatabaseFileName = @"target_one.sqlite";
我也想定义一个NSArray,我的每个目标:
I'd also like to define an NSArray for each of my targets:
NSArray * const kLabelNames = [[NSArray alloc] initWithObjects:
@"nameLabel", @"addressLabel", nil];
但是,这给了错误:初始元素不是常量。使用arrayWithObjects`也不起作用。这是因为在我的数组字符串不常量?
But this gives "error: initializer element is not constant". Using 'arrayWithObjects` doesn't work either. Is this because the strings in my array are not constants?
如何建立一个数组作为全局常量?谢谢你。
How can I set up an array as a global constant? Thanks.
推荐答案
如果你想要一组常数,包括NS类型,考虑将它们放在一个单例。
If you want a set of constants that includes NS types, consider placing them all in a singleton.
您可以有一个头文件和多个实现文件(一个目标一个)。只要所有执行在头文件中声明的类,你应该罚款。
You could have a single header file, and multiple implementation files (one per target). As long as that all implement the class declared in the header file you should be fine.
这篇关于如何使用一个NSArray作为一个全局常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!