0

I need to extract code from

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           [self doSomethingWithData:data];
                           outstandingRequests--;
                           if (outstandingRequests == 0) {
                               [self doSomethingElse];
                           }
                       }];

to

void AsynchronousRequestCompletionHandler (NSURLResponse *response, NSData *data, NSError *error)
{
  ...    
}

but I can't: enter image description here

in xamarin iOS i did it: instead this

TextLogin.ShouldChangeCharacters += () => { some code...};

made this:

TextLogin.ShouldChangeCharacters += LoginAndPassValidator;

// ... some code

bool LoginAndPassValidator (UITextField textField, NSRange range, string replacementString)
{
    BtnLogin.Enabled = (!String.IsNullOrWhiteSpace (TextLogin.Text)) && (!String.IsNullOrEmpty (TextPass.Text));
    if (BtnLogin.Enabled) {
        BtnLogin.SetTitleColor (UIColor.Black, UIControlState.Normal);
    } else {
        BtnLogin.SetTitleColor (UIColor.Gray, UIControlState.Normal);
    }
    return true;
}

Can I make this extraction in obj C?

4

2 回答 2

0
[NSURLConnection sendAsynchronousRequest:request
                               queue:[NSOperationQueue mainQueue]
                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                       AsynchronousRequestCompletionHandler(response, data, error);
                   }];

编辑:

你可以用 C++ 做一些方便的事情:

template <class T>
void SendAsyncRequest(NSURLRequest* request, NSOperationQueue* opQueue, T func)
{
        [NSURLConnection sendAsynchronousRequest:request
                               queue:opQueue
                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                       func(response, data, error);
                   }];
}

然后你就可以调用它(不要忘记将 .m 重命名为 .mm):

SendAyncRequest(request, [NSOperationQueue mainQueue], AsynchronousRequestCompletionHandler);

这是你需要的吗?

于 2013-11-04T14:50:49.473 回答
0

注意参数是一个块,而不是一个方法。所以你不能使用方法(或函数)来代替它。

typedef void (^ResponseCompletionBlock) (NSURLResponse *, NSData *, NSError *);

ResponseCompletionBlock completionHandler = ^(NSURLResponse *response, NSData *data, NSError *error) {
    [self doSomethingWithData:data];
    outstandingRequests--;

    if (outstandingRequests == 0) {
        [self doSomethingElse];
    }
};

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:completionHandler];

如果你想在一个方法中处理它,最简单的解决方案是

ResponseCompletionBlock completionHandler = ^(NSURLResponse *response, NSData *data, NSError *error) {
     [self asynchronousRequestCompletionHandlerForResponse:response 
                                                   withData:data
                                                   andError:error];
};

我不建议在这种情况下使用纯 C 函数。不要在 Obj-C 中使用 C# 语言模式/命名规则。

于 2013-12-06T11:36:10.577 回答