2015-10-24 53 views
-1

我在我的iOS项目中有url变量,我希望它指向http://localhost:3000/api当我在DEBUG模式下构建项目时,但是当我为RELEASE构建项目时,我想url变量指向http://example.com/api如何使用两个相同名称的变量

所以对于我已经勾勒出以下

#ifdef DEBUG 
    // want to use this variable on DEBUG build 
    NSURL *url = [NSURL URLWithString:@"http://localhost:3000/api/"]; 
#endif 
    // want to use this variable on RELEASE build 
    NSURL *url = [NSURL URLWithString:@"http://example.com/api/"]; 

但Xcode的抱怨,我已经声明了一个url变量。

回答

3

尝试

#ifdef DEBUG 
    // want to use this variable on DEBUG build 
    NSURL *url = [NSURL URLWithString:@"http://localhost:3000/api/"]; 
#else 
    // want to use this variable on RELEASE build 
    NSURL *url = [NSURL URLWithString:@"http://example.com/api/"]; 
#endif 
3

为什么不这样做:

NSURL *url; 
#ifdef DEBUG 
// want to use this variable on DEBUG build 
url = [NSURL URLWithString:@"http://localhost:3000/api/"]; 
#endif 
// want to use this variable on RELEASE build 
url = [NSURL URLWithString:@"http://example.com/api/"]; 
1

嗯,你没声明它。想一想:这是有条件的代码。那么代码实际上看起来好像是否定义了DEBUG?它看起来像这样:

NSURL *url = [NSURL URLWithString:@"http://localhost:3000/api/"]; 
NSURL *url = [NSURL URLWithString:@"http://example.com/api/"]; 

那么,这是非法的。

4

你应该定义设定值之前变量 试试这个代码:

NSURL *url; 
#ifdef DEBUG 
// want to use this variable on DEBUG build 
url = [NSURL URLWithString:@"http://localhost:3000/api/"]; 
#else 
// want to use this variable on RELEASE build 
url = [NSURL URLWithString:@"http://example.com/api/"]; 
#endif 
相关问题