Obj-C:实现自定义子类的委托

发布于 2024-11-14 08:56:58 字数 492 浏览 4 评论 0原文

ASIHTTPRequest 文档 中,它说:

对于更复杂的情况,或者您想要在后台解析响应的情况,请为每种类型的请求创建 ASIHTTPRequest 的最小子类,并覆盖 requestFinished: 和 failureWithError:。

因此,我创建了一个最小的子类 MyRequest ,它将在后台线程中处理解析。我没有重写 ASIHTTPRequestDelegate,因此我的委托类实现了该协议。但是,这些委托方法会传递 ASIHTTPRequest(我希望将其视为)MyRequest,以利用新功能。使用 Objective-C 的继承规则来处理这个问题的正确方法是什么?

In the ASIHTTPRequest documentation, it says:

For more complex situations, or where you want to parse the response in the background, create a minimal subclass of ASIHTTPRequest for each type of request, and override requestFinished: and failWithError:.

So I've created a minimal subclass MyRequest that will handle parsing in a background thread. I haven't overridden ASIHTTPRequestDelegate, so my delegate class implements that protocol. However, those delegate methods pass a ASIHTTPRequest, which is (and I'd like to be treated as) a MyRequest, to take advantage of the new functionality. What's the right way to handle this with Objective-C's inheritance rules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2024-11-21 08:56:59

委托大概知道它正在使用 MyRequest。在这种情况下,它可以在方法主体中进行检查强制转换:

- (void)someDelegateMethod:(ASIHTTPRequest *)sender
{
    NSAssert([sender isKindOfClass:[MyRequest class]],
            @"%s: Request %@ must be kind of MyRequest.", __func__, sender);
    MyRequest *req = (MyRequest *)sender;
    /* use |req| */
}

The delegate presumably knows it's working with a MyRequest. In that case, it can do a checked cast in the body of the method:

- (void)someDelegateMethod:(ASIHTTPRequest *)sender
{
    NSAssert([sender isKindOfClass:[MyRequest class]],
            @"%s: Request %@ must be kind of MyRequest.", __func__, sender);
    MyRequest *req = (MyRequest *)sender;
    /* use |req| */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文