Casebash
Casebash

Reputation: 118842

Raw strings like Python's in Objective-C

Does Objective-C have raw strings like Python's?

Clarification: a raw string doesn't interpret escape sequences like \n: both the slash and the "n" are separate characters in the string. From the linked Python tutorial:

>>> print 'C:\some\name'  # here \n means newline!
C:\some
ame
>>> print r'C:\some\name'  # note the r before the quote
C:\some\name

Upvotes: 4

Views: 2803

Answers (6)

karlbsm
karlbsm

Reputation: 397

You can use stringize macro.

#define MAKE_STRING(x) @#x

NSString *expendedString = MAKE_STRING(
hello world
"even quotes will be escaped"
);

The preprocess result is

NSString *expendedString = @"hello world \"even quotes will be escaped\"";

As you can see, double quotes are escaped, however new lines are ignored. This feature is very suitable to paste some JS code in Objective-C files. Using this feature is safe if you are using C99.

source:

Upvotes: 5

Seva Alekseyev
Seva Alekseyev

Reputation: 61378

Like everyone said, raw ANSI strings are very easy. Just use simple C strings, or C++ std::string if you feel like compiling Objective C++.

However, the native string format of Cocoa is UCS-2 - fixed-width 2-byte characters. NSStrings are stored, internally, as UCS-2, i. e. as arrays of unsigned short. (Just like in Win32 and in Java, by the way.) The systemwide aliases for that datatype are unichar and UniChar. Here's where things become tricky.

GCC includes a wchar_t datatype, and lets you define a raw wide-char string constant like this:

wchar_t *ws = L"This a wide-char string.";

However, by default, this datatype is defined as 4-byte int and therefore is not the same as Cocoa's unichar! You can override that by specifying the following compiler option:

-fshort-wchar

but then you lose the wide-char C RTL functions (wcslen(), wcscpy(), etc.) - the RTL was compiled without that option and assumes 4-byte wchar_t. It's not particularly hard to reimplement these functions by hand. Your call.

Once you have a truly 2-byte wchar_t raw strings, you can trivially convert them to NSStrings and back:

wchar_t *ws = L"Hello";
NSString *s = [NSString stringWithCharacters:(const unichar*)ws length:5];

Unlike all other [stringWithXXX] methods, this one does not involve any codepage conversions.

Upvotes: 3

Dave DeLong
Dave DeLong

Reputation: 243156

From your link explaining what you mean by "raw string", the answer is: there is no built in method for what you are asking.

However, you can replace occurrences of one string with another string, so you can replace @"\n" with @"\\n", for example. That should get you close to what you're seeking.

Upvotes: 4

Yuji
Yuji

Reputation: 34185

Objective-C is a superset of C. So, the answer is yes. You can write

char* string="hello world";

anywhere. You can then turn it into an NSString later by

NSString* nsstring=[NSString stringWithUTF8String:string];

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 190942

If you mean C-style strings, then yes.

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135548

Objective-C is a strict superset of C so you are free to use char * and char[] wherever you want (if that's what you call raw strings).

Upvotes: 1

Related Questions