Authoritative description of ObjectiveC string lit

2019-01-20 12:34发布

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?

2条回答
狗以群分
2楼-- · 2019-01-20 12:58
神经病院院长
3楼-- · 2019-01-20 13:03

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...

查看更多
登录 后发表回答