AFNetworking 不兼容的块指针类型发送
AFNetworking Incompatible block pointer types sending
我正在使用 AFNetworking 通过 Windows 身份验证访问 URL。我正在使用 ASIHTTPRequest 像这样登录:
-(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)'
我如何创建一个使用用户和密码登录和存储这些凭据的方法...这可以通过 AFNetworking 实现,它肯定可以通过 ASIHTTPRequest
我觉得你是misunderstanding blocks。您正在从回调块 returning TRUE
/FALSE
,不是 您的方法。这些块将在未来的某个时间点执行,它们的 return 类型是 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");
}
至于你的设计,我不会 return 是对还是错,你必须找到另一种方法来管理成功和失败的场景,可能 看起来像像这样(我对你的整体设计了解不够,无法给出明确的答案):
-(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
我正在使用 AFNetworking 通过 Windows 身份验证访问 URL。我正在使用 ASIHTTPRequest 像这样登录:
-(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)'
我如何创建一个使用用户和密码登录和存储这些凭据的方法...这可以通过 AFNetworking 实现,它肯定可以通过 ASIHTTPRequest
我觉得你是misunderstanding blocks。您正在从回调块 returning TRUE
/FALSE
,不是 您的方法。这些块将在未来的某个时间点执行,它们的 return 类型是 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");
}
至于你的设计,我不会 return 是对还是错,你必须找到另一种方法来管理成功和失败的场景,可能 看起来像像这样(我对你的整体设计了解不够,无法给出明确的答案):
-(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