宏定义抽取技巧

 1 // 用UTF-8对参数进行编码
 2 #define encode(str) [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
 3 
 4 
 5 // ...代表参数个数不限定
 6 #define getUrl(...) [@"http://192.168.1.106:8080/MJServer/" stringByAppendingFormat:__VA_ARGS__]
 7 
 8 
 9 //如果处于调试状态(这个宏定义一般放在.pch文件中,作为全局定义)
10 #ifdef DEBUG
11 #define MGLog(…) NSLog(__VA_ARGS__)
12 #else
13 #define MGLog(…)
14 #endif
15 
16 //使用的时候
17  NSString *username = encode(self.useranmeField.text);
18  NSString *pwd = encode(self.pwdField.text);
19    
20 NSString *urlString = getUrl(@"login?username=%@&pwd=%@&type=JSON", username, pwd);
原文地址:https://www.cnblogs.com/yyh123/p/3347691.html