宏保留静态值

时间:2017-03-06 12:14:58

标签: ios objective-c

下面是我用来选择IP的宏。弹出一个alertview,用户尝试登录,因为相应的IP设置为从服务器获取数据。

static NSString *updateProfileDetails_URL=@"http://%@/api/Home/editProfile/ios/1";


#define getServerURl(url,selectdServer)[[NSString stringWithFormat:@"%@",url] stringByReplacingOccurrencesOfString:@"%@"
withString:([selectdServer isEqualToString:@"live"] ?@"live_ip/folder_name":@"demo_ip/folder_name" )]
  1. 我选择了' demo_ip'选项登录。
  2. 已退出。
  3. 选择'直播'选项现在。 这里的问题是在某些地方调用demo_ip。请帮忙。
  4. 由于

2 个答案:

答案 0 :(得分:1)

您可以尝试这样:

#define USE_TEST_URL 1   // use 1 for test and 0 for live

#if USE_TEST_URL  // define test urls here

#define API_URL @"http://...<TEST URL>"

#else  // define live urls here

#define API_URL @"http://... <LIVE URL>"

#endif

NSString *url =[[NSString stringWithFormat:@"%@",API_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:1)

Macro没有任何问题,当您调用getServerURl(...)

时,传递参数可能会出现问题

当您需要直播时,请务必通过“直播”。在getServerURl(...)的第二个参数中!因为你有条件地比较生活&#39;小写值。

有关详细信息:在源文件甚至编译之前,宏将由预处理器替换为其值。因此,您无法在运行时更改宏的值。

enter image description here