如何将附加参数传递给 ASIHTTPRequest didFinishSelector 选择器

发布于 2024-12-03 23:33:39 字数 1205 浏览 1 评论 0原文

我使用 ASIHTTPRequest 为我们的其余 api 构建 api。

Api 将所有请求包装到它自己的方法中。

- (void) foo:(id)caller 
     andSuccessCallback:(SEL)successCallback 
     andFailureCallback:(SEL)failureCallback {

    _currentCaller=caller;
    _currentSuccessCallback=successCallback;
    _currentFailureCallback=failureCallback;

    NSString *urlString = @"http://localhost/foo";
    NSURL *url= [NSURL URLWithString: urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(fooSuccess:)];
    [request startAsynchronous];
}

- (void) fooSuccess:(ASIHTTPRequest *)request {
    if (statusCode >=400) {
     if ([_currentCaller respondsToSelector:_currentFailureCallback])
        [_currentCaller performSelector:_currentFailureCallback withObject:statusCode withObject:message];
} else {
    if ([_currentCaller respondsToSelector:_currentSuccessCallback])
        [_currentCaller performSelector:_currentSuccessCallback];
} 
    [request release];
}

目前,为了传递回调选择器,我使用类私有变量 _currentCaller _currentSuccessCallback _currentFailureCallback=failureCallback

我可以将它们直接传递给 fooSuccess 回调吗?

Im building api for our rest api using ASIHTTPRequest.

Api wraps all requests to its own methods.

- (void) foo:(id)caller 
     andSuccessCallback:(SEL)successCallback 
     andFailureCallback:(SEL)failureCallback {

    _currentCaller=caller;
    _currentSuccessCallback=successCallback;
    _currentFailureCallback=failureCallback;

    NSString *urlString = @"http://localhost/foo";
    NSURL *url= [NSURL URLWithString: urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(fooSuccess:)];
    [request startAsynchronous];
}

- (void) fooSuccess:(ASIHTTPRequest *)request {
    if (statusCode >=400) {
     if ([_currentCaller respondsToSelector:_currentFailureCallback])
        [_currentCaller performSelector:_currentFailureCallback withObject:statusCode withObject:message];
} else {
    if ([_currentCaller respondsToSelector:_currentSuccessCallback])
        [_currentCaller performSelector:_currentSuccessCallback];
} 
    [request release];
}

Currently to pass callback selector I use class private variables _currentCaller _currentSuccessCallback _currentFailureCallback=failureCallback

Can i pass them to fooSuccess callback directly?

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

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

发布评论

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

评论(1

她比我温柔 2024-12-10 23:33:39

您可以通过填充 NSDictionary 并将其设置为请求的 userInfo 属性来传递对象。

You can pass objects by populating a NSDictionary and setting it as the request's userInfo property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文