AFNetworking发送不兼容的块指针types

我正在使用AFNetworking通过Windows身份validation访问URL。 我正在使用ASIHTTPRequest来login,如下所示:

-(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

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

我如何创build一个方法,使用用户名和密码来login和存储这些凭证…这是可能的AFNetworking,它确实是与ASIHTTPRequest

我想你是误解块 。 你从callback块返回TRUE / FALSE而不是你的方法。 这些块将在未来的某个时间点被执行,并且它们的返回types是void 。 尝试运行此代码以查看该命令:

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

至于你的devise,我不会回答真假,你必须find另一种方法来pipe理成功和失败的情况,这可能看起来像这样(我不知道你的整体devise足以给出明确的答案) :

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

你可以在这里看到bock声明: https : //github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPRequestOperation.h