AFNetworking不兼容的块指针类型发送 [英] AFNetworking Incompatible block pointer types sending

查看:158
本文介绍了AFNetworking不兼容的块指针类型发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking通过Windows身份验证访问URL。我正在使用ASIHTTPRequest这样登录:

I am using AFNetworking to access a URL with Windows Authentication. I was using ASIHTTPRequest to login like so:

-(BOOL)User:(NSString *)user andPassWordExists:(NSString *)password
{
    NSURL *url = [NSURL URLWithString:kIP];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setUseSessionPersistence:YES];
    [request setUseKeychainPersistence:NO];
    [request setUsername:user];
    [request setPassword:password];
    [request setDomain:@"domain"];
    [request startSynchronous];

    NSError *loginError = [request error];
    if(loginError == nil){
        return true;
    }else{
        return false;
    }

}

现在我正在尝试做AFNetworking也是如此,这是我从此示例中得出的结果: http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18385

and now I am trying to do the same thing with AFNetworking and this what I came up with from this example: http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18385

-(BOOL)User:(NSString *)user andPassWordExists:(NSString *)password
{
    NSURL *url = [NSURL URLWithString:kIP];

    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                         timeoutInterval:90.0];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceNone]];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];


    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        return true;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        return false;

    }];
    [operation start];
}

但这给了我两个错误:

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:148:46: Incompatible block pointer types sending 'int (^)(AFHTTPRequestOperation *__strong, __strong id)' to parameter of type 'void (^)(AFHTTPRequestOperation *__strong, __strong id)'

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:152:15: Incompatible block pointer types sending 'int (^)(AFHTTPRequestOperation *__strong, NSError *__strong)' to parameter of type 'void (^)(AFHTTPRequestOperation *__strong, NSError *__strong)'

我如何创建一种使用用户名和密码登录和存储这些凭据的方法。 .AFNetworking可以实现,肯定是ASIHTTPRequest

How Can I create a method that will use user and password to login and store those credentials...this is possible with AFNetworking, it sure was with ASIHTTPRequest

推荐答案

我认为您是误解区块。您从回调块返回 TRUE / FALSE ,而不是不是。这些块将在将来的某个时刻执行,它们的返回类型为 void 。尝试运行以下代码以查看顺序:

I think you are misunderstanding blocks. You are returning TRUE/FALSE from the callback blocks, not your method. Those blocks will be executed at some point in the future, and their return type is void. Try running this code to see the order:

-(void)User:(NSString *)user andPassWordExists:(NSString *)password
{
    // construct your request

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"In success block");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"In Fail block");
    }];
    [operation start];

    NSLog(@"Network op started");
}

对于您的设计,我不会返回true或false,您必须找到另一种管理成功和失败方案的方法,可能看起来像这样(我对您的总体设计了解不够,无法给出确切的答案):

As to your design, I would not return true or false, you have to find another way to manage the success and fail scenarios, which could look something like this (I don't know enough about your overall design to give a definitive answer):

-(void)User:(NSString *)user andPassWordExists:(NSString *)password
{
    // construct your request

    __weak typeof(self) weakSelf = self;
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // called at somepoint in the future if the request is success full
        [weakSelf handleSuccess:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // called at somepoint in the future if the request fails
        [weakSelf handleFail:error];
    }];
    [operation start];
}

- (void)handleSuccess(id)response {
    // process the response
}

- (void) handleFail:(NSError*)error {
    // evaluate error
}

您请在此处查看博克声明: https://github.com/AFNetworking/AFNetworking /blob/master/AFNetworking/AFHTTPRequestOperation.h

You see the bock declaration here: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPRequestOperation.h

这篇关于AFNetworking不兼容的块指针类型发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆