Objective-C相当于类方法中Java的匿名类

hpi*_*que 17 java delegates objective-c anonymous-types

我想在Objective-C中的类方法中设置对象的委托.伪代码:

+ (ClassWithDelegate*) myStaticMethod {
    if (myObject == nil) {
        myObject = [[ClassWithDelegate alloc] init];
        // myObject.delegate = ?
    }
    return myObject;
}
Run Code Online (Sandbox Code Playgroud)

在Java中,我只需创建一个实现委托协议的匿名类.如何在Objective-C中做类似的事情?

基本上我想避免创建一个单独的类(和文件)来实现一个简单的委托协议.

Amo*_*kar 20

正如JeremyP正确地说的那样,Objective C中没有像Java那样的匿名类.

但在Java中,匿名类主要用于实现单个方法接口或我们也称为功能接口.

我们这样做是为了避免必须在类**中实现接口**,仅用于一个最常用于监听器,观察者和事件处理程序的方法实现.

这主要是因为在Java中缺少匿名的第一类函数(在版本8和项目lambda之前).

Objective C有一个叫做块的东西,你可以直接传递一个包含该单个方法实现的块,而不是一个包含它的空类.

例:

在Java中使用匿名类

//Functional interface
interface SomethingHandler 
{
  void handle(Object argument);
}

//a method that accepts the handler in some other class
class SomeOtherClass
{ 
  void doSomethingWithCompletionHandler(SomethingHandler h){
      // do the work that may consume some time in a separate thread may be.
      // when work is done call the handler with the result which could be any object
      h.handler(result);
  };
}

// Somewhere else in some other class, in some other code
// passing the handler after instantiating someObj as an object of SomeOtherClass which can use the handler as needed
SomeOtherClass someObj = new SomeOtherClass();
someObj.doSomethingWithCompletionHandler( new SomethingHandler()
                        {
                              void handle(Object argument)
                              {
                                // handle the event using the argument
                              }
                         });
Run Code Online (Sandbox Code Playgroud)

在目标C中

// declare the handler block 
typedef void (^SomethingHandler)(id argument){}

// this interface is different than Java interface  which are similar to Protocols
@interface SomeOtherClass
 -(void)doSomethingWithCompletionHandler:(SomethingHandler)h;
@end

@implementation SomeOtherClass
 -(void)doSomethingWithCompletionHandler:(SomethingHandler)h
 {
          // do the work that may consume some time in a separate thread may be.
          // when work is done call the handler with the result which could be any object
          h(result);
 }

@end

  // passing the handler after instantiating someObj as an object of SomeOtherClass which can use the handler as needed

SomeOtherClass* someObj = [[SomeOtherClass alloc] init]; // ARC :)

[someObj doSomethingWithCompletionHandler:^(id argument)
                                            {
                                               // handle the event using the argument
                                            }];
Run Code Online (Sandbox Code Playgroud)


Jer*_*myP 15

Objective-C目前没有匿名类.

通常,您可以使用现有对象.例如,对于NSTableViewDataSource,您可以在文档或视图控制器中实现这些方法,并将其作为委托传递.

或者您可以让对象本身实现协议,并在默认情况下使其成为自己的委托.

或者发送委托消息的方法可以检查nil委托,并在那种情况下做一些合理的事情.

或者,您可以在要创建需要委托的对象的实现文件中声明和定义类.