Graphics Programs Reference
In-Depth Information
plication. Select
Debug RandomPossessions
at the top of the log navigator to see your
console output in the editor area (
Figure 2.6
).
Figure 2.6 Console output
Now let's go back and take a closer look at the code in your
main
function.
Creating strings
First, notice the
@"One"
argument in the first
addObject:
message sent to
items
.
[items addObject:@"One"];
In Objective-C, when you want a hard-coded string, you prefix a character string with an
@
symbol. This creates an instance of
NSString
that holds the character string.
But, wait - aren't instances created by sending
alloc
to a class? Yes, but the
@
prefix is
a special case just for the
NSString
class. It is convenient shorthand for creating strings.
The following code shows three such uses, and each is completely valid Objective-C,
where
length
is a message you can send to an instance of
NSString
:
NSString *myString = @"Hello, World!";
int len = [myString length];
len = [@"Hello, World!" length];
myString = [[NSString alloc] initWithString:@"Hello, World!"];
len = [myString length];
Format strings
Next, let's look at the
NSLog
function we used to print to the console.
NSLog
takes a
variable number of arguments and prints a string to the console. The first argument is re-
quired and must be an
NSString
instance. This instance is called the
format string
, and