13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2: Object-Oriented Programming 31Listing 2-5. Deeply-Nested Messages[myView addSubview:[[LabelView alloc] initWithTitle:[[String alloc] initWithCString: "title"]]];Now, you might feel like you’re trapped somewhere not unlike the setting of Inception, tumblingthrough calls within calls within calls. Well, that’s actually a fairly apt analogy: the nested dreamswithin the movie perform in exactly the same manner as the nested methods shown previously.Within the first call, you dive into another, and then into another, and so on. Then, as the lastone completes, its result is fed back into the former, which feeds its own result back up untileverything has resolved.In places like this, <strong>Objective</strong>-C’s bracketed syntax actually makes things easier; each openingbrace indicates the start of a new method call. When a closing brace appears, the result of thatbracketed statement will appear to become part of the surrounding statement. In the previousexample, there are the following steps:1. In looking at [myView addSubview:, you see an opening brace. Dive downa level.2. Another opening brace, followed by another, which wraps [LabelViewalloc]. So that method is evaluated.3. The result is followed by initWithTitle: and more opening braces,down another level.4. Here you see the same construct again: you evaluate [String alloc]and the result is paired with initWithCString:"title".5. Now you’ve found a closing brace: evaluate the initWithCString: calland go back up a level.6. This is followed by another closing brace: the result is passed to-initWithTitle:.7. The last closing brace: the result of the previous step is applied to-addSubview:, and you step up again out of the entire expression.Memory ManagementHaving covered the basics of objects’ creation and use, let’s now look at memory management.After all, as you saw earlier, all <strong>Objective</strong>-C objects are allocated on the heap, so they musttherefore be deallocated somewhere if you are not to run out of resources.Back in the dim and distant past of the 1980s and the early 1990s, <strong>Objective</strong>-C used a memorymanagement model quite similar to that of C: if you allocated something, you had to rememberto deallocate it. And you needed to be careful about what you were given, too. Somethingreturned an object? It needed to be deallocated using the -free message 4 .4For example, see the source for the original web browser, written in <strong>Objective</strong>-C in 1991, atwww.w3.org/History/1991-WWW-NeXT/Implementation/.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!