我已经习惯将@optional
委托方法发送到:
if ([self.delegate respondsToSelector:@selector(method:)]) {
[self.delegate method:obj];
}
效果很好,但是如果有很多委托的方法,它看起来像重复respondToSelector:
代码......
在某些时候我将respondsToSelector:
放在单独的方法中:
//ignore warning
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (void)performDelegateSelector:(SEL)selector withObject:(id)obj {
if ([self.delegate respondsToSelector:selector]) {
[self.delegate performSelector:selector withObject:obj];
}
}
结果我只有一个respondToSelector:check,但它看起来仍然没有很好的改进。
[self performDelegateSelector:@selector(method:) withObject:self];
你怎么看?使用一些帮助器或类别来包装发送所有@optional
委托方法是否有意义,或者它是不应该改进的东西?
答案 0 :(得分:13)
性能方面,它取决于您对委托的用途,是否直接影响UI(在这种情况下,您希望有更快的响应时间)等。
良好的速度优化是在首次设置委托时在数据结构中注册委托实现的所有方法。你可以这样做:
struct {
unsigned int myFirstMethod:1;
unsigned int mySecondMethod:1;
} delegateRespondsTo;
-(void)setDelegate:(id<MyProtocol>)delegate
{
self.delegate = delegate;
delegateRespondsTo.myFirstMethod = [delegate respondsToSelector:@selector(myFirstMethod)];
delegateRespondsTo.mySecondMethod = [delegate respondsToSelector:@selector(mySecondMethod)];
}
然后你可以简单地做
if ( delegateRespondsTo.myFirstMethod )
[self.delegate myFirstMethod];
检查这个很棒的answer是否有更详尽的解释。
答案 1 :(得分:4)
你可以使用蹦床。 trampoline是一个将消息转发给另一个对象的对象。这是一个简单的DelegateTrampoline。
#import <objc/runtime.h>
@interface DelegateTrampoline : NSObject
- (id)initWithProtocol:(Protocol *)protocol delegate:(id)delegate;
@end
#import "DelegateTrampoline.h"
@interface DelegateTrampoline ()
@property (nonatomic) Protocol *protocol;
@property (nonatomic, weak) id delegate;
@end
@implementation DelegateTrampoline
- (id)initWithProtocol:(Protocol *)protocol delegate:(id)delegate {
self = [super init];
if (self) {
_protocol = protocol;
_delegate = delegate;
}
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
// Look for a required method
struct objc_method_description desc = protocol_getMethodDescription(self.protocol, selector, YES, YES);
if (desc.name == NULL) {
// Maybe it's optional
desc = protocol_getMethodDescription(self.protocol, selector, NO, YES);
}
if (desc.name == NULL) {
[self doesNotRecognizeSelector:selector]; // Raises NSInvalidArgumentException
return nil;
}
else {
return [NSMethodSignature signatureWithObjCTypes:desc.types];
}
}
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];
if ([self.delegate respondsToSelector:selector]) {
[invocation setTarget:self.delegate];
[invocation invoke];
}
}
@end
以下是您将如何使用它:
@interface MyObject ()
@property (nonatomic) id delegateTrampoline;
@end
...
self.delegateTrampoline = [[DelegateTrampoline alloc] initWithProtocol:@protocol(MyProtocol)
delegate:delegate];
[self.delegateTrampoline myobject:self didSomethingAtIndex:1];
@end
一些注意事项:
NSInvocation
对象,它比简单的方法调用慢几百倍。也就是说,除非你在紧密的循环中调用你的委托方法,否则它可能不是问题。delegateTrampoline
声明为id
类型。这允许您将任意选择器传递给它。但这也意味着编译器无法检测您是否将选择器传递给协议中不存在的delegateTrampoline
。如果你这样做,你将在运行时崩溃。编译器可以检测你是否传递了一个完全未知的选择器,因此它会捕获简单的拼写错误。答案 2 :(得分:1)
除了@micantox提供的答案之外,我想补充一点,您可以使用struct
代替NS_OPTIONS
来定义bitmask
。
typedef NS_OPTIONS (NSUInteger, MyDelegateOptions)
{
MyDelegateOptionDidFinishedWithTaskOne = 1 << 0,
MyDelegateOptionDidFinishedWithTaskTwo = 1 << 1,
MyDelegateOptionDidFinishedWithTaskThree = 1 << 2
};
然后,您可以创建可以容纳bitmask
的属性:
@property (nonatomic, assign) MyDelegateOptions delegateOptions;
并检查您的设置器中是否委托respondsToSelector:
- (void)setDelegate:(id<MyDelegateProtocol>)delegate
{
_delegate = delegate;
if ([delegate respondsToSelector:@selector(myDelegateDidFinishedWithTaskOne:)]) {
{
self.delegateOptions = self.delegateOptions | MyDelegateOptionDidFinishedWithTaskOne;
}
if ([delegate respondsToSelector:@selector(myDelegateDidFinishedWithTaskTwo:)]) {
{
self.delegateOptions = self.delegateOptions | MyDelegateOptionDidFinishedWithTaskTwo;
}
if ([delegate respondsToSelector:@selector(myDelegateDidFinishedWithTaskThree:)]) {
{
self.delegateOptions = self.delegateOptions | MyDelegateOptionDidFinishedWithTaskThree;
}
}
然后,您可以使用bitwise AND
if (self.delegateOptions & MyDelegateOptionDidFinishedWithTaskOne) {
[self.delegate myDelegateDidFinishedWithTaskTwo:myResult];
}
正如您所看到的,bitwise AND
的使用将返回true
,如果MyDelegateOptionDidFinishedWithTaskOne
的位设置为 1 ,即使设置了其他位也是如此。
因此,如果您认为在您的设置器中检查了delegate
并且基于此delegateOptions
保持 00000111 的位模式,并且您的MyDelegateOptionDidFinishedWithTaskOne
代表位模式 00000001 然后只需使用bitwise AND
00000111&amp; 00000001 = 00000001
您将获得true
条件声明。
值得注意的是,如果您只需要检查delegate
一次或两次,那就太过分了。