
Chapter 2: Extensions to C14
This prints “Hello, Objective-C!” to the console. If you’ve used C at all, you have undoubtedly
encountered printf() in your travels. NSLog() is a Cocoa function that works very much like printf().
Just like printf(), NSLog() takes a string as its first argument. This string can contain format
specifiers (such as %d), and the function takes additional parameters that match the format
specifiers. printf() plugs these extra parameters into the string before it gets printed.
As we’ve said before, Objective-C is just C with a little bit of special sauce, so you’re welcome to
use printf() instead of NSLog() if you want. We recommend NSLog(), however, because it adds
features such as time and date stamps, as well as automatically appending the newline ('\n')
character for you.
The NS Prefix: A Prescription Against Name Collisions
You might be thinking that NSLog() is kind of a strange name for a function. What is that “NS”
doing there? It turns out that Cocoa prefixes all its function, constant, and type names with
“NS”. This prefix tells you the function comes from Cocoa instead of some other toolkit.
The prefix helps prevent name collisions, big problems that result when the same identifier is
used for two different things. If Cocoa had named this function Log(), there’s a good chance
the name would clash with a Log() function created by some innocent programmer somewhere.
When a program containing Log() is built with Cocoa included, Xcode complains that Log() is
defined multiple times, and sadness results.
Now that you have an idea why a prefix is a good idea, you might wonder about the specific
choice: why “NS” instead of “Cocoa,” for example? Well, the “NS” prefix dates back from the
time when the toolkit was called NextSTEP and was the product of NeXT Software (formerly
NeXT, Inc.), which was acquired by Apple in 1996. Rather than break compatibility with code
already written for NextSTEP, Apple just continued to use the “NS” prefix. It’s a historical
curiosity now, like your appendix.
Cocoa has staked its claim on the “NS” prefix, so obviously, you should not prefix any of your
own variables or function names with “NS”. If you do, you will confuse the readers of your code,
making them think your stuff actually belongs to Cocoa. Also, your code might break in the
future if Apple happens to add a function to Cocoa with the same name as yours. There is no
centralized prefix registry, so you can pick your own prefix. Many people prefix names with their
initials or company names. To make our examples a little simpler, we won’t use a prefix for the
code in this book.
NSString: Where it’s @
Let’s take another look at that NSLog() statement: NSLog (@"Hello, Objective-C!");
Did you notice the at sign before the string? It’s not a typo that made it past our vigilant editors.
The at sign is one of the features that Objective-C adds to standard C. A string in double quotes
preceded by an at sign means that the quoted string should be treated as a Cocoa NSString
element.
So what’s an NSString element? Peel the “NS” prefix off the name and you see a familiar term:
“String.” You already know that a string is a sequence of characters, usually human-readable, so
you can probably guess (correctly) that an NSString is a sequence of characters in Cocoa.