Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiline NSString constants

437 views
Skip to first unread message

kelvSYC

unread,
May 28, 2009, 6:37:10 PM5/28/09
to
Apparently, you cannot declare a multiline NSString constant (which is
useful for, say, if you had an ultralong SQL statement that you want to
break up over multiple lines to make it more readable). I've worked
around it by using +stringWithFormat:, but that seems a bit tedious
(and you have to keep track of how many pieces you have).

Is there another, better way?

Also related to this, IIRC in C++ if you have two string literals it's
treated as if they were concatenated (not sure about C). Does this
hold in Objective-C(++) with NSString constants?

Allen Brunson

unread,
May 28, 2009, 8:13:36 PM5/28/09
to
kelvSYC wrote:
> Apparently, you cannot declare a multiline NSString constant (which is
> useful for, say, if you had an ultralong SQL statement that you want to
> break up over multiple lines to make it more readable).

you also can’t include non-ASCII characters in NSString literals. my
workaround is to declare them as C strings, then convert to NSStrings at
runtime, if needed.

Gregory Weston

unread,
May 28, 2009, 8:34:20 PM5/28/09
to
In article <280520091637103597%no.e...@here.com>,
kelvSYC <no.e...@here.com> wrote:

> Apparently, you cannot declare a multiline NSString constant (which is
> useful for, say, if you had an ultralong SQL statement that you want to
> break up over multiple lines to make it more readable). I've worked
> around it by using +stringWithFormat:, but that seems a bit tedious
> (and you have to keep track of how many pieces you have).
>
> Is there another, better way?

Probably the best way is to not hard-code them into your software.
Include them in a plist file in the bundle's resource folder or some
other solution.

--
I saw a truck today that had "AAA Batteries / Delivered and Installed" on the
side. My first thought was: That's a really weird business model. How many
inept people have urgent need of skinny little battery cells?

Chmeee

unread,
May 29, 2009, 6:18:22 AM5/29/09
to
On May 28, 5:37 pm, kelvSYC <no.em...@here.com> wrote:
> Also related to this, IIRC in C++ if you have two string literals it's
> treated as if they were concatenated (not sure about C).  Does this
> hold in Objective-C(++) with NSString constants?

This holds true for me with stock GCC. I can't say offhand about
Apple's GCC.

David Phillip Oster

unread,
May 29, 2009, 10:33:15 AM5/29/09
to
In article <PM00046B0...@genevieve-2.unknown.dom>,
Allen Brunson <noe...@spam.com.invalid> wrote:

@"This is a multiline NSString Constant\n"
"of course you can do it, and, in OS X 10.5,\n"
"Xcode 3, you can püt nön-äscïï characters in\n"
"your string constants. But: you must make sure\n"
"that you've correctly set the encoding of source\n"
"file of the project to UTF-8 in Xcode's View > Text menu.\n"
"Better: use NSLocalizedString() with ASCII keys, and values in a\n"
"UTF-16 strings file."

Allen Brunson

unread,
May 30, 2009, 12:44:09 AM5/30/09
to
David Phillip Oster wrote:
> @"This is a multiline NSString Constant\n"
> "of course you can do it, and, in OS X 10.5,\n"
> "Xcode 3, you can püt nön-äscïï characters in\n"

> "your string constants. But: you must make sure\n"
> "that you've correctly set the encoding of source\n"
> "file of the project to UTF-8 in Xcode's View > Text menu.\n"

ig. that may be true, but i’d never do it! my philosophy is that source
files should always be plain old 7-bit ascii, the way god intended.

you can follow this rule and embed utf8 characters in C strings using escape
sequences.

0 new messages