tags:

views:

30

answers:

2

Hi,

I need to call a method through the object created by the interface ie

id<MyProtocol>obj;

Now i have created this obj in my main class where i am not implementing the methods of this protocol but i need to access the method which is already implemented in someother class.I am now calling the method as follows

[obj load]; 

in my main class in the applicationDidFinishLaunching, but i not able to access the method? Pls do suggest me the correct way of calling the methods through protocols...

+2  A: 

A protocol implements nothing. It only describes a set of messages that the object should respond to. Your obj object belongs to some class. This class needs to implement methods described in MyProtocol.

Edit

A protocol is not implemented by a specific class. Any class that claims to conform to a protocol must implement its methods. Any object that claims to conform to a protocol must belong to a class that implements its methods.

In your case, obj is a ClassB, so ClassB must implement methods described by MyProtocol, either directly or through inheritance.

mouviciel
MyProtocol is already taken by a class say ClassA and implemented with the method.Now i need to call the method through the object created by MyProtocol ie obj in ClassB. How do i do that?
Cathy
A: 

[obj load] is OK. If you want to shut up the compiler you can cast it to id:

[(id)obj load];

But if you know you'll need to call the -load method, maybe you should add the -load method to the protocol, or make another protocol that has the -load method e.g.

@protocol Loadable
-(void)load;
@end

...
id<MyProtocol, Loadable> obj;
KennyTM
MyProtocol is already taken by a class say ClassA and implemented with the method.Now i need to call the method ie load method through the object created by MyProtocol ie obj in ClassB. How do i do that?
Cathy