好吧所以我想创建一个公共函数,如果对象存在,将返回YES,符合协议并响应选择器。我知道@selector的typedef是SEL,但是@protocol
的typedef是什么BOOL conforms(id object, ? prototype, SEL action) { return (object != nil && [object conformsToProtocol:prototype] && [object respondsToSelector:action]); }
我希望能够像以下一样调用此函数:
if(conforms(delegate, @protocol(UIScrollViewDelegate), @selector(touchesBegan:withEvent:))) { [delegate touchesBegan:touches withEvent:event]; }
答案 0 :(得分:4)
您正在寻找Protocol
对象:
BOOL conforms(id object, Protocol *protocol, SEL action) {
return (object != nil &&
[object conformsToProtocol:protocol] &&
[object respondsToSelector:action]);
}