3

The Apple ObjectiveC description implies that NSStrings exist by showing examples, and that NSString literals are written as

 @ "<string content>"

Amazingly, that's all it says, and these only show up in examples without any discussion. Is that document really the only reference document?

Digging around, I found this blog which seems to have a lot of useful information. But is it right?

Is there an authoritative document describing precisely what one can say (syntax) in an NSString literal, and what it means (semantics)? For instance, is the syntax of the content of a NSString literal identical to that of a C(99) string literal? Is the syntax different in ObjectiveC++? Can I prefix the string literal content with a width specification as I can for other type of C99/C++ strings, or is there only one kind of NSString literal? What are the dark corners?

Or is my only choice to dig into GCC4's and Clang's implementation to find out?

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Unfortunately there is no Standard for ObjC as there is for C++ or C. Apple being the main user and driver behind the language, their compiler implementation (the ObjC bits of Clang) is the _de facto_ Standard. [TOCPL](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/) is about as close as you get to a prose version, and obviously it's nowhere near the precision of either of those other languages' Standards (and it tends to lag behind the compiler). – jscs May 27 '12 at 22:13
  • 4
    Great. Here we are in the 21st century, using languages that are poorly defined, that run on millions of devices, because nobody will bother. And we wonder why software construction is hard? I don't even know what's legal to write, let alone what it means! Forgive my frustration. – Ira Baxter May 27 '12 at 22:51
  • 1
    No, I agree with you. It would be nice to have the language standard written down. – jscs May 27 '12 at 22:53
  • I don't know when this became available, but here is as definitive a treatment as I would think you could want. http://clang.llvm.org/docs/ObjectiveCLiterals.html – CuriousRabbit May 12 '14 at 16:08

1 Answers1

2

Given the choice between the docs and a random blog, I'd lean to believing the docs.

There isn't much written because there isn't much to say; @".." is a static string constant. It is created at compile time, written into the binary into a form that is mapped into memory in a read-only fashion, and is even more immutable than an NSString instance itself (constant NSStrings don't implement retain counting at all, for example; there is no allocation).

There is no real syntax associated with them save for the normal syntax the compiler uses with plain old C strings. I.e. you can do most of the various stringification gunk, etc...

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    Well, I'd believe the docs but they actually don't say anything; examples of NSString literals are provided only "by accident". The reference could say, NSString literals have the same syntax and interpretation as C string literals, except the result is UTF-16. And that's my guess, but I'm trying to build an analyzer and don't want to guess, or play empirical experimental games. – Ira Baxter May 27 '12 at 19:45
  • ... here's a simple question that I can answer empirically but would rather have a standard to fall back on: I can clearly write @"abc". Can I write @ "abc" (space between @ and ")? – Ira Baxter May 27 '12 at 19:48
  • Questions like that, you pretty much have to answer via reading the source. – bbum May 27 '12 at 20:34