当发生特殊事情时,我正在尝试为一个应该称之为委托(如果有的话)的类实现委托。
从维基百科我有这个代码示例:
@implementation TCScrollView
-(void)scrollToPoint:(NSPoint)to;
{
BOOL shouldScroll = YES;
// If we have a delegate, and that delegate indeed does implement our delegate method,
if(delegate && [delegate respondsToSelector:@selector(scrollView:shouldScrollToPoint:)])
shouldScroll = [delegate scrollView:self shouldScrollToPoint:to]; // ask it if it's okay to scroll to this point.
if(!shouldScroll) return; // If not, ignore the scroll request.
/// Scrolling code omitted.
}
@end
如果我自己尝试此操作,我会收到一条警告,指出我在委托上调用的方法未找到。当然不是,因为委托只是被 id 引用。它可以是任何东西。当然在运行时这会很好,因为我检查它是否响应选择器。但我不想要 Xcode 中的警告。有更好的模式吗?