Question

The following code snippet is in Java. How can we get anonymous class implementations in Objective C?

//MyDelegate is an interface

MyDelegate delegate=new MyDelegate() {
@Override
public void onDelegateResponse(int responseType,NSString data)
  {


  }
};

//DFNodesControl is a normal java class
DFNodesControl obj=new DFNodesControl();
obj.makeRequest(delegate,currentNode);
Was it helpful?

Solution

I think you want to use anonymous classes.

Objective-C doesn't have this exact feature as of now. See the question "Objective-C equivalent to Java's anonymous classes in class methods".

You can create blocks in Objective-C, though. They work like regular functions and can be passed around and reused. You'd have to change your interface to take a block as a delegate instead of an object in this case. Or you use a simple wrapper class; Among provides an example for this in the aforementioned SO question.

See Apple's block programming docs for details.

OTHER TIPS

In Objective-C you create a class which implements the protocol and then you implement each of the required methods specified in the @protocol.

There is no 'default' implementation for an @protocol that you can use (and thus override the default implementations).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top