开发者

Compound/Complex C-String in Xcode/Cocoa

I am creating an SQLite3 program. I am creating the database within the code. I have about 15 columns. Example below:

NSString *createSQL = "CREATE TABL开发者_开发问答E IF NOT EXISTS FIELDS (DATE TEXT PRIMARY KEY,
                   NAME TEXT,           /*COLUMN #1 */
                   CLASS TEXT,          /*COLUMN #2 */
                   ...
                   COMPLETED TEXT);";  /*COLUMN #15 */

When I compile I am getting "missing terminating character error. I did some research and saw that because it is a C-String it will process the CR\LF as a continuation character. I don't want to create a line of codes that scrolls the right most margin of the screen every time I need to do something with SQL. Is there a way that I can create a string that can be broken up on separate lines without getting this error?


Two things:

  1. You need a @ character before the string begins. NSString literals @"look like this".

  2. Your string is split over multiple lines, which is confusing the compiler (this is the error you're seeing). You need to have it be entirely on one line. If you want to split it like this, wrap each line in quotes, like this:

_

NSString * foo = @"foo "
                  "bar "
                  "baz";

Or do as Barry says here and use a trailing backslash at the end of each line to indicate continuation.


Hello i think you forget to place @ character at the begining of string as required in Objective C

NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (DATE TEXT PRIMARY KEY, NAME TEXT, //COLUMN #1 CLASS TEXT, //COLUMN #2 ... COMPLETED TEXT);"; //COLUMN #15


In Objective-C, just as in C, you can use the line continuation marker \ to break a string literal across multiple lines. You example would be (note the @ to indicate an NSString literal):

NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (DATE TEXT PRIMARY KEY, \
                   NAME TEXT,  \       /*COLUMN #1 */
                   CLASS TEXT, \       /*COLUMN #2 */
                   ...         \
                   COMPLETED TEXT);";  /*COLUMN #15 */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜