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.

76CHAPTER 4: <strong>Objective</strong>-C Language FeaturesObject BObject AObject CFigure 4-1. A reference cycleObject A can’t be deallocated until Object C is, and Object C will never be deallocated as longas Object B has a strong reference to it. Finally, Object B will never be deallocated until Object Ais deallocated. This is a reference cycle and it will cause memory in an application to leak.In Figure 4-2, the strong reference Object C had to Object A (symbolized by a solid line) hasbeen replaced with a weak reference (symbolized by a dashed line).Object BObject AObject CFigure 4-2. Breaking a reference cycleWeak references do not prevent objects from being deallocated, so Object A is deallocated, thenObject B, and finally Object C.If you’re familiar with C or C++, you’ll likely notice a problem with weak references. If an objectcan be deallocated at any moment, and you have a weak reference to that object, then you canend up with a dangling pointer. With ARC compiling for iOS 5 and OS X 10.7 (or higher), weakreferences to objects become nil once those objects are deallocated; these are therefore knownas zeroing weak references.iOS 4 and OS X 10.6 do not support the __weak keyword used to denote weak references so youmust use __unsafe_unretained instead. As its name implies, it is unsafe to rely on a referenceto an unsafe, unretained object. This may seem dangerous, but as long as you are aware of thislimitation, the application code you write should be fine.Let’s take a quick review of the different types of variables in <strong>Objective</strong>-C. There are localvariables, which belong to their enclosing scope (simply: the surrounding pair of curly braces,whether a function, if statement, for statement, etc.) and are only in scope during the executionof that scope. Additionally, there are instance variables, which belong to object instances andare valid until the object instance is deallocated.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!