I'm trying to write something similar to this (sorry if the sample is not 100% correct, im just writing off the top of me head):
interface Handler
{
void doSomething ( );
}
otherclass
{
void othermethod ( Handler handler )
{
// Handler do something
}
}
开发者_运维知识库
So in ObjectiveC I made the following:
@protocol Handler
- (void) doSomething;
@end
// Other class
- (void) othermethod: (Handler*) handler
{
// Do something
}
But I get the following error on the othermethod declaration line: Expected ')' before hander.
I know there is no syntax error here (I can replace Handler* with NSObject* and the error goes away), so obviously my use of a protocol in this case is incorrect.
Could anyone point me to the proper way of creating a C# like interface?
You want to use a type like this:
id <Handler> obj
This means "any object (type id
) that implements the Handler
protocol". The protocol goes in between the greater than/less than signs. You can also use a declaration like NSObject <Handler> *obj
, which means "any object of type NSObject
or a subclass that implements the Handler
protocol".
You can also declare an object that implements several protocols like so:
id <Handler, OtherHandler> obj
精彩评论