Book Image

Objective-C Memory Management Essentials

Book Image

Objective-C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Associated objects


In the Objective-C 2.0 runtime used by apps in iOS and 64-bit Mac OS X, you're allowed to set an association from any object to another. The object, in this case, without support from instance variables or methods can have a random set of extra properties set by the key at runtime, shown as follows:

objc_setAssociatedObject(myObject, myKey, myValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

You can use this if you want to set a property from outside an object. If you would be an object and your t-shirt color a property of yours, it would be like someone changing its color from outside your house, and you wouldn't even notice it.

You should use it in similar circumstances, where you want to keep the object away for knowing, supporting, or being involved while you set a property from other parts of the program. Associated objects should not be the method you want to use at the top of your head as lack of type information makes it easy for a crash to appear due to incorrect typing...