views:

32

answers:

2

Hi,

I have a program where inheritance of protocols are there say:

@protocol A
-(void)methodA
@end

The protocol which inherits:

@protocol B<A>
-(void)methodB
@end

The class which implements @protocolA method is

@interface classB<B>

@end

@implementation classB
-(void)methodA
{
  //somecode
}
@end

Now i wanted the methodA to be called from Some other class:

    @implementation SomeotherClass

{
   //call of methodA
   //????

id<A>obj=[[classB alloc]init];//i have tried it
[obj methodA];// even this is not working 
}

How to do that?

+1  A: 

Just send the message as usual:

SomeotherClass *obj = [[[SomeotherClass alloc] init] autorelease];
[obj methodA];

Since instances of classB implement protocol B, they also claim to respond to -methodA. The following compiles and runs without any problems:

MyClass.h:

#import <Foundation/Foundation.h>

@protocol A
-(void) methodA;
@end

@protocol B <A>
-(void) methodB;
@end

@interface MyClass : NSObject <B>
{
}

@end

MyClass.m:

#import "MyClass.h"

@implementation MyClass

-(void) methodA
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

-(void) methodB
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end

the code:

MyClass *obj = [[MyClass alloc] init];
[obj methodA];
[obj methodB];
[obj release];
Costique
I have tried, it is not working. I am not able to access it even if i call the method through the object of protocolA.I have edited my question at the last part.Pls suggest me how to solve the problem....
Cathy
What exactly is happening? Compiler complaining, linker failing or the program crashing?
Costique
I expanded my answer by including the whole code sample.
Costique
Thanks for the answer, at ending of the code i want to access the method in another class not in MyClass where i implement the method.I want that method to be called in a class where i dont implement that method and where the protocol<B>/<A> is not extended.How to do it?
Cathy
A: 

You don't say how it fails to work. I would guess that there's a compilation error as you don't implement methodB in your classB.

Another possibility: what happens after you've initialised classB in SomeOtherClass? Are you sure that you get a valid object back? If it returned a nil the run time would be perfectly within its rights to do nothing when you sent the methodA message to it.

Stephen Darlington