Is it recommended to add properties in categories to an Objective-C class? Is there an official Apple link?
-
1possible duplicate of [Objective-C: Property / instance variable in category](http://stackoverflow.com/questions/8733104/objective-c-property-instance-variable-in-category) – dan Jul 20 '15 at 15:09
4 Answers
First of all I want to say that Peter Segerblom's answer is not correct. But maybe Daji-Djan's comment is misleading.
From the very beginning:
A declared-property is solely a declaration of two (one – for readonly properties) methods. Period.
- This means that it is a declaration.
- This means that it refers to methods.
Therefore you can add declared-properties in a category.
But if the property is explicitly or implicitly (Apple: automatically) synthesized, it will synthesize an ivar, if there is none. This is impossible, because it would change the memory footprint of instance objects and a point in time, instance objects are already allocated.
So you have to do one of the things below:
- Already having an ivar. (What makes it a bit meaningless.)
- No synthesization of the declared-property.
However, this can be useful for computed properties, for properties, whose values are stored in another object, for properties, whose values are stored as associated object, for …

- 16,582
- 3
- 35
- 50
You can do this with associated objects. Although this is not recommended. This link has everything you need to know.
http://nshipster.com/associated-objects/
Section out of the blog:
Associated objects should be seen as a method of last resort, rather than a solution in search of a problem (and really, categories themselves really shouldn't be at the top of the toolchain to begin with).

- 2,985
- 20
- 28
You can easily do that -- if apple does this, I don't know. They often expose properties in categories though..
anyway, if it makes sense for your design, go ahead! Remember though that while you can add properties, you can't add variables to existing classes.
so either have computed properties, that are a kind of 'convenience wrapper' around existing functionality OR add variables using associated storage

- 49,552
- 17
- 113
- 135
You can't add properties to categories. I think this has something to do with the way memory is handled. Adding instance variables to a already existing object would change the size of the object.

- 2,773
- 1
- 19
- 24
-
1thats not right - you can of course add computed properties and by leverging the runtime even stored properties – Daij-Djan Jul 20 '15 at 15:09
-
It is completely correct, because he asked whether properties can be added in a category. He did not ask, whether properties can be (explicitly or implicitly) synthesized in a category. – Amin Negm-Awad Jul 20 '15 at 15:10
-
Well you can add the setter any getter but you can't allocate memory in a category right? Except in methods... – Peter Segerblom Jul 20 '15 at 15:11
-
-
You can allocate memory. You cannot allocate memory "*inside*" the instance object. – Amin Negm-Awad Jul 20 '15 at 15:19
-
1@AminNegm-Awad thanks for clarifying that if that now was unclear in some way. – Peter Segerblom Jul 20 '15 at 15:35
-
It hasn't been a big mistake, because the usual thing you do, is to synthesize a declared-property. However, it was formally not correct. – Amin Negm-Awad Jul 20 '15 at 17:27