C/Objective-Cで複数行にわたって文字列文字を分割する方法

3701 ワード

How to split a string literal across multiple lines in C/Objective-C?
I have a pretty long sqlite query:長い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";

How can I break it in a number of lines to make it easier to read? どのようにして複数行でそれを破って読みやすくしますか?If I do the following:次の操作を実行するとします.
const char *sql_query = "SELECT word_id
                        FROM table1, table2
                        WHERE table2.word_id = table1.word_id
                        ORDER BY table1.word ASC";

I am getting an error. 私は間違いを受け取った.
Is there a way to write queries in multiple lines? 複数行でクエリーを作成する方法はありますか?
1階
参照先:https://stackoom.com/question/3LPy/C-Objective-Cで複数行にわたって文字列文字を分割する方法
2階
I am having this problem all the time,so I made a tiny tool to convert text to an escaped multi-line Objective-C string:私はずっとこの問題を抱えていたので、テキストをエスケープされた複数行Objective-C文字列に変換する小さなツールを作りました.
http://multilineobjc.herokuapp.com/http://multilineobjc.herokuapp.com/
Hope this saves you some time. 時間を節約できることを願っています.
#3階
There's a trick you can do with the pre-processor. プリプロセッサでテクニックを作ることができます.It has the potential down sides that it will collapse white-space, and could be confusing for people reading the code. 潜在的な欠点があり、白い空間を崩壊させ、コードを読む人を困惑させる可能性があります.But, it has the up side that you don't need to escape quote characters inside it. ただし、引用符文字をエスケープする必要がないというメリットがあります.
#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
);

The preprocessor turns this into:プリプロセッサは、次のように変換します.
const char *sql_query = "SELECT word_id FROM table1, table2 WHERE table2.word_id = table1.word_id ORDER BY table1.word ASC";

I've used this trick when I was writing some unit tests that had large literal strings containing JSON. JSONの大きな文字列を含むセルテストを作成するときに、このテクニックを使ったことがあります.It meant that I didn't have to escape every quote character".これは私がすべての参照文字を避ける必要がないことを意味します.
#4階
Extending the Quote idea for Objective-C:拡張Objective-Cの参照構想:
#define NSStringMultiline(...) [[NSString alloc] initWithCString:#__VA_ARGS__ encoding:NSUTF8StringEncoding]

NSString *sql = NSStringMultiline(
    SELECT name, age
    FROM users
    WHERE loggedin = true
);

#5階
あなたもそうすることができます
NSString * query = @"SELECT * FROM foo "
                   @"WHERE "
                     @"bar = 42 "
                     @"AND baz = datetime() "
                   @"ORDER BY fizbit ASC";

#6階
You could also go into XCode -> Preferences, select the Indentation tab, and turn on Line Wrapping. XCode->Preferencesに入り、Indentationタブを選択してLine Wrappingを開くこともできます.
That way, you won't have to type anything extra, and it will work for the stuff you already wrote. これにより、追加のコンテンツを入力する必要がなくなり、作成したコンテンツに適用されます.:-):-)
One annoying thing though is... でも悩ましいのは...
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
}