0

I'm finding it hard to believe that there isn't a straightforward way for a class to declare its protected properties in the .m file (to avoid polluting the .h file with variables that only subclasses need know about)..

Expressed in code, I want accomplish something like this:

// SuperClass.h
NOTHING!

// SuperClass.m
@interface SuperClass
@property (nonatomic, retain) variable;
@end

// SubClass.m
// do something with variable

update: this kind of happens in UIGestureRecognizer.. see the subclassing notes:

You may create a subclass that UIGestureRecognizer that recognizes a distinctive gesture—for example, a “check mark” gesture. If you are going to create such a concrete gesture recognizer, be sure to import the UIGestureRecognizerSubclass.h header file. This header declares all the methods and properties a subclass must either override, call, or reset.

From Carl Veazey: basically the idea is that all properties/methods that the subclass should be aware of are encapsulated in a seperate .h file.. I guess that addresses the pollution problem in a different way.

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
  • How exactly are your derived classes going to know about stuff that's in the `m` file? The entire idea of `.h` and `.m` files is to provide a *definition* and then a hidden *implementation*. – Moo-Juice Apr 30 '13 at 08:56
  • i think i see what you're saying.. so let's say company x is selling a piece of code where they only show the .h file and hide everything else.. the consumers of this code will have to know what variables/methods that they can inherit.. – abbood Apr 30 '13 at 09:00
  • @Moo-Juice please see update to question – abbood Apr 30 '13 at 12:14
  • I don't really see why you consider protected/public definitions in header files as pollution. It's the way it is, for C, Objective-C, and C++. And yes, 3rd party libraries (where you do not get the source) are typically a dll/.so (compiled) together with the header files. – Moo-Juice Apr 30 '13 at 12:23
  • maybe you're right. I'm just a little frustrated, now that I'm trying to structure my code so that it can be easily subclassed, I'm finding many many opinions/methods about how this should be done. For example see the way Apple did it with UIGestureReognizer (in my update above).. [PSPDFKit](http://pspdfkit.com/), a famous PDF library does it another [way](http://pspdfkit.com/documentation/Classes/PSPDFViewController.html#//api/name/overrideClassNames).. I was hoping to find a standard/obvious way to do it. But then again maybe I should just stop complaining and start coding :) – abbood Apr 30 '13 at 12:30
  • possible duplicate of [Protected methods in objective-c](http://stackoverflow.com/questions/3725857/protected-methods-in-objective-c) – Mike Weller Apr 30 '13 at 14:34
  • This is a common question, see the second answer in the duplicate link. You essentially want to declare your "protected" properties in a category, perhaps in a separate header "SuperClass_Protected.h" which only subclasses import. The compiler won't enforce the "protectedness" but users of your code should see the "Private" in the names and know what's going on. – Mike Weller Apr 30 '13 at 14:36
  • @MikeWeller I think I just found out what caused my confusion in the first place.. basically this 'pollution' relates to when a subclass inherits from a superclass.. (it's not specific to obj-c per se).. to avoid it, i must use the design pattern principle: prefer composition over inheritance.. so I should have this subclass use an interface (or in obj-c parlance: a protocol).. that's how I can avoid this whole pollution business – abbood May 05 '13 at 03:34

1 Answers1

0

You can implement Categories in .m Class.

Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
  • 1
    not good enough.. you can't save instance variables in categories without some horrific work arounds – abbood Apr 30 '13 at 08:59
  • by [horrific work arounds](http://stackoverflow.com/a/4147242/766570) i mean [associative reference](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/uid/TP40001418-CH3g-SW5) – abbood Apr 30 '13 at 12:16