Associated objects and lazy code

Discussion in 'iOS Programming' started by nashyo, Mar 17, 2013.

  1. nashyo macrumors 6502


    Oct 1, 2010
    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.
  2. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    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.
  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I've never even heard of this before... it sounds kind of like Java reflection...

Share This Page