Objective-c在宏里拼接字符串

时间:2023-03-10 08:12:39
Objective-c在宏里拼接字符串

//正式服务器
#define API_DOMAIN @"www.online.com"
//测试服务器
//#define DOMAINXX @"192.168.0.10"

#define API_SYSTEM @"http://"API_DOMAIN@"/system/"
#define API_USER @"http://"API_DOMAIN@"/user/"

API_SYSTEM 宏展开后是: @"http://"@"www.online.com"@"/system/"
编译器会自动将字符中连接起来,目的实现。

c语言下的实现:
//正式服务器
#define API_DOMAIN "www.online.com"
//测试服务器
//#define DOMAINXX "192.168.0.10"

#define API_SYSTEM "http://"API_DOMAIN"/system/"

#define API_USER "http://"API_DOMAIN"/user/"

参考:
http://*.com/questions/8844981/how-to-expand-a-macro-into-nsstring-without-using-any-string-concatenation-at-ru

http://*.com/questions/798221/c-macros-to-create-strings

最后,感谢万能的google,万能的*。