jrdioko
jrdioko

Reputation: 33496

Obj-C: Implementing delegate of custom subclass

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?

Upvotes: 1

Views: 231

Answers (1)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

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| */
}

Upvotes: 3

Related Questions