Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Isn't objc_get/set associatedObject a bit of a hack? Does anyone use it? There are similar calls in AppKit took right?

Seems a bit like viewWithTag which always feel like a bit of a lazy approach too.
 
Isn't objc_get/set associatedObject a bit of a hack? Does anyone use it? There are similar calls in AppKit took right?

Seems a bit like viewWithTag which always feel like a bit of a lazy approach too.

Associative storage is a very powerful feature that lets you essentially add instance variables to objects after they are created.

It has a little bit of overhead associated with it, but it can be lifesaver.

I created a category of NSObject that adds setAssociatedObject:forKey and getAssociatedObject:forKey methods for any object.

I've used it when I need to save additional data in objects that the system creates (I have 3 places in an app my company is developing where I need to display a customized UIMenuController. By using associative storage, I can tell in the IBAction methods for the menu controller which case invoked the menu. It made for a cleaner solution.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.