首页 文章

如何在C / Objective-C中跨多行拆分字符串文字?

提问于
浏览
287

我有一个很长的sqlite查询:

const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC";

如何在多行中打破它以便于阅读?如果我执行以下操作:

const char *sql_query = "SELECT word_id
                        FROM table1, table2
                        WHERE table2.word_id = table1.word_id
                        ORDER BY table1.word ASC";

我收到了一个错误 .

有没有办法在多行中编写查询?

7 回答

  • 515

    我一直有这个问题,所以我做了一个小工具来将文本转换为转义的多行Objective-C字符串:

    http://multilineobjc.herokuapp.com/

    希望这能为您节省一些时间 .

  • 17

    扩展Objective-C的Quote想法:

    #define NSStringMultiline(...) [[NSString alloc] initWithCString:#__VA_ARGS__ encoding:NSUTF8StringEncoding]
    
    NSString *sql = NSStringMultiline(
        SELECT name, age
        FROM users
        WHERE loggedin = true
    );
    
  • 19

    堆栈的另一个解决方案,将.m文件更改为.mm,使其成为Objective-C并使用C原始文字,如下所示:

    const char *sql_query = R"(SELECT word_id
                               FROM table1, table2
                               WHERE table2.word_id = table1.word_id
                               ORDER BY table1.word ASC)";
    

    原始文字会忽略所有内容,直到终止序列,在默认情况下为括号 - 引号 .

    如果括号引号序列必须出现在某处的字符串中,您也可以轻松指定自定义分隔符,如下所示:

    const char *sql_query = R"T3RM!N8(
                                      SELECT word_id
                                      FROM table1, table2
                                      WHERE table2.word_id = table1.word_id
                                      ORDER BY table1.word ASC
                             )T3RM!N8";
    
  • 3

    你也可以这样做:

    NSString * query = @"SELECT * FROM foo "
                       @"WHERE "
                         @"bar = 42 "
                         @"AND baz = datetime() "
                       @"ORDER BY fizbit ASC";
    
  • 24

    有两种方法可以在多行上拆分字符串:

    使用\

    C中的所有行都可以使用\分割成多行 .

    平原C:

    char *my_string = "Line 1 \
                       Line 2";
    

    Objective-C的:

    NSString *my_string = @"Line1 \
                            Line2";
    

    更好的方法

    有一种更好的方法适用于字符串 .

    平原C:

    char *my_string = "Line 1 "
                      "Line 2";
    

    Objective-C的:

    NSString *my_string = @"Line1 "
                           "Line2";    // the second @ is optional
    

    第二种方法更好,因为没有包含很多空格 . 但是,对于SQL查询,两者都是可能的 .

    注意:使用#define,您必须添加一个额外的''来连接两个字符串:

    平原C:

    #define kMyString "Line 1"\
                      "Line 2"
    
  • 4

    你可以用预处理器做一个技巧 .
    它有潜在的缺点,它会崩溃白色空间,并且可能会让阅读代码的人感到困惑 .
    但是,它有一个好处,你不需要在其中转义引号字符 .

    #define QUOTE(...) #__VA_ARGS__
    const char *sql_query = QUOTE(
        SELECT word_id
        FROM table1, table2
        WHERE table2.word_id = table1.word_id
        ORDER BY table1.word ASC
    );
    

    预处理器将其转换为:

    const char *sql_query = "SELECT word_id FROM table1, table2 WHERE table2.word_id = table1.word_id ORDER BY table1.word ASC";
    

    当我编写一些包含JSON的大型文字字符串的单元测试时,我已经使用过这个技巧 . 这意味着我没有必要逃避每个引用字符“ .

  • 96

    您也可以进入XCode - > Preferences,选择Indentation选项卡,然后打开Line Wrapping .

    这样,您就不必再输入任何额外内容,它将适用于您已编写的内容 . :-)

    但令人烦恼的是......

    if (you're long on indentation
        && short on windows) {
                then your code will
                    end up squished
                         against th
                             e side
                                 li
                                  k
                                  e
    
                                  t
                                  h
                                  i
                                  s
    }
    

相关问题