Objective-C中的全局常量 [英] Global constants in Objective-C
问题描述
我有一个名为 Constants.h 的文件:
extern NSString * const BASE_URL;
和 Constants.m :
#ifdef DEBUG
NSString * const BASE_URL = @http://www.example.org;
#else
NSString * const BASE_URL = @http:// localhost;
#endif
第一个问题:如何将DEBUG切换为 True
和 False
?
我有一个视图控制器文件 MyViewController.m
:
#导入MyViewController.h
#importConstants.h
//这不起作用。请参阅上面的错误。
static NSString * const ANOTHER_URL = [NSString stringWithFormat:@%@%@,BASE_URL,@/ path /];
@implementation HomeViewController
[...]
代码不起作用,并返回此错误:
错误:语义问题:初始化元素不是编译时间常数
我该如何解决这个问题?
$ b
UPDATE 1
我需要将几个全局字符串变量与其他字符串结合起来创建各种url。
现在 Constants.m
是:
#importConstants.h
#ifdef DEBUG
#define DEF_BASE_URLhttp://www.example.org/
#else
#define DEF_BASE_URLhttp:// localhost /
#endif
NSString * const BASE_URL =(NSString *)CFSTR(DEF_BASE_URL);
NSString * const API_URL =(NSString *)CFSTR(DEF_BASE_URLapi /);
NSString * const API_SETTINGS_URL =(NSString *)CFSTR(API_URLsettings /);
但最后一行有错误解析错误:预期')' STRONG>。
也许我只能用宏使用CFSTR。我需要找到一种方法来获得我所有的全局变量。解决方案A:
就个人而言,我只会使用 ANOTHER_URL
的函数。
<如果你真的想要一个常量:你应该能够通过
来使用cstring连接规则, #define
,然后通过 CFSTR()
管道: // defs.h
extern NSString * const BASE_URL;
extern NSString * const ANOTHER_URL;
// defs.m
#ifdef DEBUG
#define DEF_BASE_URLhttp://www.example.org
#else
#define DEF_BASE_URLhttp:// localhost
#endif
NSString * const BASE_URL =(NSString *)CFSTR(DEF_BASE_URL);
NSString * const ANOTHER_URL =(NSString *)CFSTR(DEF_BASE_URL/ path /);
解决方案C:
如果您想通过初始化创建一个,您还可以在C ++ / ObjC ++翻译中完成一个函数/方法local static(然后在需要时使用C或ObjC可见性):
NSString * URL(){
static NSString * const ANOTHER_URL = [NSString stringWithFormat:@%@%@,BASE_URL,@/ path / ];
返回ANOTHER_URL;
}
I've a file called Constants.h:
extern NSString * const BASE_URL;
and Constants.m:
#ifdef DEBUG
NSString * const BASE_URL = @"http://www.example.org ";
#else
NSString * const BASE_URL = @"http://localhost";
#endif
First question: How can I switch DEBUG to be True
and False
?
I've a view controller file MyViewController.m
:
#import "MyViewController.h"
#import "Constants.h"
// this doesn't works. see above for the error.
static NSString * const ANOTHER_URL = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"];
@implementation HomeViewController
[...]
The code doesn't work and returns me this error:
error: Semantic Issue: Initializer element is not a compile-time constant
How can I fix this?
I need to combine several global string variabile with other strings for create various urls.
UPDATE 1
Now Constants.m
is:
#import "Constants.h"
#ifdef DEBUG
#define DEF_BASE_URL "http://www.example.org/"
#else
#define DEF_BASE_URL "http://localhost/"
#endif
NSString * const BASE_URL = (NSString*)CFSTR(DEF_BASE_URL);
NSString * const API_URL = (NSString*)CFSTR(DEF_BASE_URL "api/");
NSString * const API_SETTINGS_URL = (NSString*)CFSTR(API_URL "settings/");
But there is an error on the last line Parse error: expected ')'. Probably I can use CFSTR only with macros. I need to find a way for have all my global variables.
Solution A:
Personally, I would just use a function for ANOTHER_URL
.
Solution B:
If you really want a constant: You should be able to use cstring concatenation rules via #define
, then pipe that through CFSTR()
:
// defs.h
extern NSString * const BASE_URL;
extern NSString * const ANOTHER_URL;
// defs.m
#ifdef DEBUG
#define DEF_BASE_URL "http://www.example.org"
#else
#define DEF_BASE_URL "http://localhost"
#endif
NSString * const BASE_URL = (NSString*)CFSTR(DEF_BASE_URL);
NSString * const ANOTHER_URL = (NSString*)CFSTR(DEF_BASE_URL "/path/");
Solution C:
If you want to create just one via initialization, you can also accomplish a function/method local static in C++/ObjC++ translations (then use C or ObjC visibility, where needed):
NSString * URL() {
static NSString * const ANOTHER_URL = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/path/"];
return ANOTHER_URL;
}
这篇关于Objective-C中的全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!