01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

66 CHAPTER 3 An introduction to objects<br />

for you out of the box, and you can reuse existing classes without needing to reinvent<br />

the wheel with each application. Reuse makes you more productive, allowing you to<br />

focus on differentiating and perfecting your application instead of spending days<br />

building a basic foundation.<br />

3.5 Strings<br />

The NSString class provides a nice object-oriented interface to represent, interact<br />

with, and modify a string of characters. Unlike in a C-style char * null-terminated<br />

string or char[] array, all aspects of memory allocation, text encoding, and string<br />

manipulation are hidden from the application developer as internal implementation<br />

details of the NSString class. This allows you to worry about the more important<br />

and unique aspects of your application logic rather than the nitty-gritty details<br />

of how strings are stored in a computer or how operations such as string concatenation<br />

operate.<br />

This abstraction also means that common sources of error, such as attempting to<br />

store a 250-character string in a variable that has allocated space for only 200 characters,<br />

are easily avoided in <strong>Objective</strong>-C. Let’s start our discussion of strings by learning<br />

how to create new strings in your source code.<br />

3.5.1 Constructing strings<br />

The easiest way to create a new NSString instance is via the convenient @"..." language<br />

syntax introduced in previous chapters. For example, the statement<br />

NSString *myString = @"Hello, World!";<br />

creates a new NSString object and initializes it with the value "Hello, World!". This<br />

form of syntax, however, is a special case for the NSString class. It’s impossible to use<br />

this syntax to create instances of any other class. The use of text strings in applications<br />

is so pervasive that the <strong>Objective</strong>-C language designers decided it was beneficial to<br />

have dedicated (and concise) syntax to create them. A more generic technique to<br />

construct an object of a particular class is to send that class an alloc message to allocate<br />

memory for a new object, followed by some kind of initialization message. For<br />

example, here’s another way to create a string:<br />

NSString *myString = [NSString alloc];<br />

myString = [myString initWithString:@"Hello, World!"];<br />

This code sample explicitly demonstrates the two stages of constructing a new object<br />

in <strong>Objective</strong>-C: allocation and initialization. The first line sends the alloc (short for<br />

allocate) message to the NSString class. This causes NSString to assign enough memory<br />

to store a new string and returns a C-style pointer to that memory. At this stage,<br />

however, the object is rather blank, so the next step is to call an initialization method<br />

to initialize the object with some kind of sensible value.<br />

Many classes provide a number of initialization messages. In this example, you sent<br />

the initWithString: message, which initializes the new string object with the contents

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!