1

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?

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.