Objective-C equivalent to Java#39;s anonymous classes in class methods(Objective-C 等价于 Java 在类方法中的匿名类)
问题描述
I want to set the delegate of an object inside a class method in Objective-C. Pseudo-code:
+ (ClassWithDelegate*) myStaticMethod {
if (myObject == nil) {
myObject = [[ClassWithDelegate alloc] init];
// myObject.delegate = ?
}
return myObject;
}
In Java I would simply create an anonymous class that implemented the delegate protocol. How can I do something similar in Objective-C?
Basically I would like to avoid creating a separate class (and files) to implement a simple delegate protocol.
There are currently no anonymous classes in Objective-C.
Often you can use an already existing object. For instance, for an NSTableViewDataSource, you can implement the methods in the document or view controller and pass that as the delegate.
Or you can have the object itself implement the protocol and make it its own delegate in the default case.
Or the methods that send the delegate messages can check for a nil delegate and do something sensible in that situation.
Or you can declare and define a class inside the implementation file you are creating the object that needs a delegate.
这篇关于Objective-C 等价于 Java 在类方法中的匿名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective-C 等价于 Java 在类方法中的匿名类


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01