将多行字符串传递给宏

时间:2014-04-02 09:32:24

标签: iphone ios7

我在常量中使用宏。这是我的代码。

#define kTermsConditions(message) [NSString stringWithFormat:@"USER TERMS AND CONDITIONS

PLEASE READ THIS BEFORE ACCESSING THE INC. (“”) ONLINE  SERVICE.  BY ACCESSING THE U, YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS BELOW.  IF YOU DO NOT WISH TO BE BOUND BY THESE TERMS AND CONDITIONS, YOU MAY NOT USE THE SITE.

1. ALL PERSONS USING THIS SITE AGREE TO REPORT ON (MANIFESTS) IN ACCORDANCE WITH 49 U.S.C, AND 8 C.F.R. PARTS 217, 231, AND 251.

2. USERS MUST BE AFFILIATED WITH AN APPROVED OPERATOR.  AN CHOOSING TO USE THE SERVICES OF SUCH USER, WHETHER AN AIRCRAFT OPERATOR EMPLOYEE OR A THIRD-PARTY, IS SPECIFICALLY AWARE THAT IT IS LIABLE FOR ALL ACTIONS OR INACTION OF THE USER WHILE TRANSMITTING ON ITS BEHALF.  RELIANCE ON A REGISTERED USER IS NOT A DEFENSE FOR INCORRECT OR INCOMPLETE DATA, OR FOR PENALTY ACTIONS AGAINST THE.

3. ELIGIBILITY FOR USE OF AND ACCESS TO THIS SITE IS SUBJECT TO FINAL APPROVAL AND ACCEPTANCE BY.

但我并没有把这个冗长的多行字符串打包成宏。请指导。

2 个答案:

答案 0 :(得分:2)

您需要添加一个新的行代码字符(如果以这种方式调用),只需\。这将告诉编译器该字符串将在下一行继续。

#define kTermsConditions(message) [NSString stringWithFormat:@"USER TERMS AND CONDITIONS\
\
PLEASE READ THIS BEFORE ACCESSING THE INC. (“”) ONLINE  SERVICE.  BY ACCESSING THE U, YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS BELOW.  IF YOU DO NOT WISH TO BE BOUND BY THESE TERMS AND CONDITIONS, YOU MAY NOT USE THE SITE.\
\n\n\
1. ALL PERSONS USING THIS MCL/APIS SITE AGREE TO REPORT ON (MANIFESTS) IN ACCORDANCE WITH 49 U.S.C, AND 8 C.F.R. PARTS 217, 231, AND 251.\
\n\n\
2. USERS MUST BE AFFILIATED WITH AN APPROVED OPERATOR.  AN CHOOSING TO USE THE SERVICES OF SUCH USER, WHETHER AN AIRCRAFT OPERATOR EMPLOYEE OR A THIRD-PARTY, IS SPECIFICALLY AWARE THAT IT IS LIABLE FOR ALL ACTIONS OR INACTION OF THE USER WHILE TRANSMITTING ON ITS BEHALF.  RELIANCE ON A REGISTERED MCL/APIS USER IS NOT A DEFENSE FOR INCORRECT OR INCOMPLETE DATA, OR FOR PENALTY ACTIONS AGAINST THE.\
\n\n\
3. ELIGIBILITY FOR USE OF AND ACCESS TO THIS SITE IS SUBJECT TO FINAL APPROVAL AND ACCEPTANCE BY."]

答案 1 :(得分:0)

您需要了解的三件事:

如何在没有不可能的长线的情况下创建长字符串:这很容易。如果一个接一个地写多个字符串文字,编译器会将它们变成一个。例如

@"This "
"is "
"a "
"very "
"long "
"string."

完全相同
@"This is a very long strong."

如何创建包含换行符的字符串(仅在您需要时才执行此操作,例如对于要在多个段落中出现的文本):只需将\ n插入到字符串中即可。

如何制作非常长的宏:如果一行中的最后一个字符是反斜杠字符,后面没有任何内容(甚至不是空格字符),则编译器将该行与下一行连接起来。您可以使用它来创建长宏。像

#define MYSTRING @"This "\
    "is "\
    "a "\
    "very "\
    "long "\
    "macro. "