Resolved Objective-C: Attempt to set category property results in "unrecognized selector sent to instance"

Discussion in 'iOS Programming' started by moonman239, Jan 5, 2016.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I have a category on UIView (because there are multiple UIViews of different classes that I want having the same property), and all the category does is define a property in the .h file.
    And, as it turns out, that's exactly why I'm having an error.
  2. bjet767 macrumors 6502a

    Oct 2, 2010
    Without code it is almost impossible to understand why you are getting and error.

    Basically the reason is you are trying to send something which has not been initialized in some way.

    You can declare something in the .h file but unless it is initialized in some manner in the code it is a nil object.

    So did you answer your own question? "...that's exactly why I'm having an error."
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    Yes. You see, each category property must be implemented in the category's .m file. You can create a getter/setter, or you can use the "@dynamic" directive.
  4. Mascots, Jan 14, 2016
    Last edited: Jan 14, 2016

    Mascots macrumors 68000


    Sep 5, 2009
    Is this really resolved? You can't directly add properties to classes through a catagory, you need to be using associated objects if that is the case. This is a limitation of the Objective-C runtime - I can find you more information if you'd like.

    Adding @dynamic doesn't somehow allow the runtime to store the reference on the instance for you - it merely states that the setter and better are defined outside the superclass class, which in this case is the category. Because of that, the property was never synthesized and it's not a part of your instance. It's up to you to write the appropriate setter and getter using associated objects to set and retrieve that property from the Obj-C runtime.

Share This Page