views:

564

answers:

2

Hello folks,

I was reading how to implement private methods in Objective-C (Best way to define private methods for a class in Objective-C) and a question popped up in my mind:

How do you manage to implement protected methods, i.e. private methods that are visible to subclasses?

Suppose I have a MySuperClass with a Category containing all its private methods, and I want to implement a MySubclass overriding or calling super to one of the MySuperClass private methods. Is that possible (using the Categories approach towards implementing private methods)?

Take a look at some of this code, at the bottom there is the overriden method.

// ===========================
// = File: MySuperClass.h
// = Interface for MySuperClass
// ===========================

@interface MySuperClass : Object
...
@end

// ===========================
// = File: MySuperClass.m
// ===========================
#import "MySuperClass.h"

// =================================
// = Interface for Private methods
// =================================
@interface MySuperClass (Private)

-(void) privateInstanceMethod; 

@end

// =====================================
// = Implementation of Private methods
// =====================================
@implementation MySuperClass (Private)

-(void) privateInstanceMethod
{
  //Do something
}

@end

// ================================
// = Implementation for MySuperClass
// ================================
@implementation MySuperClass
... 
@end




// ===========================
// = File: MySubClass.h
// = Interface for MySubClass
// ===========================

@interface MySubClass : MySuperClass
...
@end


// ================================
// = Implementation for MySubClass
// ================================

#import MySubClass.h

@implementation MySubClass
//OVERRIDING a Super Private method.
-(void) privateInstanceMethod
{
  [super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible!
  //Do something else
}
@end

Hopefully somebody already figured this out.

Cheers!

+5  A: 

This GNUStep page describes one approach Section 4.5:

...The bright side of this is it allows you to simulate protected methods as well. For this, the writer of a subclass must be informed in some way about the protected methods, and they will need to put up with the compiler warnings. Alternatively, you could declare the Protected category in a separate interface file (e.g., "PointProtected.h"), and provide this interface file with the understanding that it should only be imported and used by a subclass's interface file.

nall
This is how I do it. Just put the method declarations in a category header, but implement them in the main implementation block. Then the files that need to know about the method just have to `#import` the header.
Dave DeLong
I thank you both Dave and nall. Cheers
Lio
+2  A: 

At Apple, when they build the frameworks the typical pattern is to have a public header (MyClass.h) and a private header (MyClass_private.h), and only copy the public headers into the build product. The .m file will #import both of them, of course.

NSResponder