how to add member variable and methods to in built class without subclassing it

cocoa, objective-c

Solution

For methods: you can use categories to add member methods without subclassing. It is a pretty common practice in Cocoa, to add per-framework extensions methods.

For instance variables: starting with Snow Leopard (Mac OS X 10.6), you can use associative references. You use them to simulate the addition of object instance variables to an existing class.

The Objective-C Programming Language is pretty comprehensive on what you can do with both associative references and categories.

Problem

How can I add member variables and methods to an in built class(say, NSString) without subclassing it.

Original source