0

In Objective-C I can create categories to extend a class. I can also add properties to them as they are basically only methods. However I cannot bind them with @synthesize or any other way I know. Therefore whenever I try to use them the app would crash.

Is there any use of properties in categories or are they just possible because it would break the logic of the language to forbid them. Could you give me a scenario where defining a property in a category would make sense?

gebirgsbärbel
  • 2,327
  • 1
  • 22
  • 38
  • 2
    See my answer to your other question: http://stackoverflow.com/questions/10502539/objective-c-category-and-new-ivar (Why did you ask the essentially the same question twice?) – Andrew Madsen May 08 '12 at 16:45
  • Thank you. I thought that besides a solution to my concrete problem it might als be interesting to have a more general question on what properties in categories are good for. However your answer seems to provide both already. :) – gebirgsbärbel May 08 '12 at 16:59
  • possible duplicate of [Objective-C: Property in Category](http://stackoverflow.com/questions/8733104/objective-c-property-in-category) – hfossli Jan 16 '14 at 08:24

2 Answers2

1

The main use of defining property in category is in creating class extension, with the help of category when we create class extension then there we can define property and synthesise them as well.This is the way to achieve hiding of property for public access.

You can check this for more information - http://www.techpaa.com/2012/04/adding-properties-to-categories-and.html

rishi
  • 11,779
  • 4
  • 40
  • 59
0

From The Objective-C Programming Language guide

A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing.

Since you want to

add properties to them as they are basically only methods

You can use the category to add methods to the class.

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • and what about category used for Class extensions. – rishi May 08 '12 at 16:40
  • @rishi The documentation says "Class extensions are similar". That implies Class extensions and categories are 2 different things – Jesse Black May 08 '12 at 16:41
  • 1
    class extensions and categories are indeed very different. In an extension I can define a property but and synthesize it. In a category I cannot synthesize the properties. But I can also not create ivars so that I cannot really implement the getters and setters for myself either. – gebirgsbärbel May 08 '12 at 16:45
  • Also yes I can add properties, but what are they good for if I can not bind them to any variable? – gebirgsbärbel May 08 '12 at 16:47
  • I wouldn't add properties to a category implementation. The docs state categories are for adding methods. Why bend the language to do what you have in mind? Subclassing is a good fit for adding ivars and methods to an existing class. – Jesse Black May 08 '12 at 16:56
  • @Maudicus that is basically what I wanted to know. I do not necessarily want to add properties to a category. I just wondered why it is possible, because it does not seem to fit Apples intentions of what categories should be used for... – gebirgsbärbel May 08 '12 at 17:03
  • @Maudicus - sometimes that help us a lot, check out this blog to understand its power - http://rafaelsteil.com/2011/07/15/a-useful-uiview-addition-via-categories-objective-c-ios/ – rishi May 08 '12 at 17:15
  • @rishi I understand that usage case, but in reality the blog is still creating methods to gain access to the fields of CGRect. The use of properties on that blog only creates "shorthand notation" to those methods. I am an advocate of using brackets to call messages in general. – Jesse Black May 08 '12 at 18:29