AFNetworking Incompatible block pointer types sending

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:22:30

问题


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;
    }

}

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];
}

but this gives me two errors:

/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)'

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


回答1:


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");
}

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
}

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



来源:https://stackoverflow.com/questions/29500188/afnetworking-incompatible-block-pointer-types-sending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!