2015-10-24 80 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 
相關問題