如何正确实施授权?
我正在尝试为一个类实现委托,当特殊情况发生时,该类应该调用它的委托(如果有)。
从维基百科我有这个代码示例:
@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 中出现警告。 有更好的模式吗?
I'm trying to implement delegation for a class which should call it's delegate (if any), when special things happen.
From Wikipedia I have this code example:
@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
If I try this on my own, I get a warning that the method I am calling on the delegate was not found. Of course it was not, because the delegate is just referenced by id. It could be anything. Sure at runtime that will work fine because I check if it responds to selector. But I don't want the warning in Xcode. Are there better patterns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以让委托具有实现 SomeClassDelegate 协议的 id 类型。 为此,您可以在 SomeClass 的标头(在您的情况下为 TCScrollView)中执行如下操作:
然后您可以从您的实现中调用委托上的方法:
You could let the delegate be of the id type that implements the SomeClassDelegate protocol. For this, you could in the header of your SomeClass (in your case TCScrollView), do something like this:
Then you can from your implementation, just call the method on the delegate:
跟进 drvdijk 的回答中的示例代码,如果调用委托方法时
delegate
可能为nil
,则可能会出现问题。发送到
nil
的消息的返回值为nil
(又名0.0
又名0
又名NO< /code>),因此如果
delegate
为nil
,将返回
NO
,这可能不是您的情况所需的行为。 首先检查会更安全:此外,如果您不想在将
NSObject
声明的消息发送到委托时看到编译器警告(例如respondsToSelector:
),请包含协议声明中的NSObject
协议:Following up on the sample code in drvdijk's answer, there could be a problem if there is any chance that
delegate
could benil
when you call the delegate method.The return value of a message sent to
nil
isnil
(aka0.0
aka0
akaNO
), so ifdelegate
isnil
,will return
NO
, which might not be the desired behavior in your case. It's safer to check first:Also, if you don't want to see a compiler warning when sending messages declared by
NSObject
to your delegate (such asrespondsToSelector:
), include theNSObject
protocol in your protocol declaration:使用
[NSObject PerformSelector:]
您将不再收到编译器警告。
或者创建一个协议并在头文件中声明
MyProtocol *delegate
。Use
[NSObject performSelector:]
You won't get the compiler warnings anymore.
Alternatively create a prototcol and declare
MyProtocol *delegate
in header file.