0

I´m trying to do a GET request to my web service, using AFNetworking(great framework). This is the code for the request:

   AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:@"http://mywebservice.com/service/"]];

[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSMutableURLRequest *request = [httpClient getPath:@"http://mywebservice.com/service/contacts"
parameters:@{@"accessID":self.accessId, @"name":contactName}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Success code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //Error code
}];

This causes the following issue to appear(pointing at my NSMutableURLRequest instance): Initializing 'NSMutableURLRequest *__strong' with an expression of incompatible type 'void'

I have no idea what causes this, so any help would be appreciated.

4

1 回答 1

3

这种方法在AFHTTPClient

-(void)getPath:parameters:success:failure:

什么都不返回 ( void) 并且您正在尝试将其分配给变量。

NSMutableURLRequest *request = [httpClient getPath:....

这是我们解释您的错误消息所需的所有信息:

Initializing 'NSMutableURLRequest *__strong' with an expression of incompatible type 'void'

您声明一个变量 ,request然后将其键入为NSMutableURLRequest *。对于内存管理,ARC 增加了内存语义__strong。您的变量的完整类型是`NSMutableURLRequest *__strong. 然后,您尝试将=方法的结果分配-(void)getPath:parameters:success:failure:给该变量。该方法不返回任何内容,否则称为void. void并且NSMutableRequest *不是同一类型,因此编译器会抱怨上述错误消息。

当你调用它时,这个方法实际上开始执行请求。结果作为参数提供给完成或失败块,在 HTTP 请求完成时执行。这将是执行您尝试发送的 HTTP 请求的正确方法:

[httpClient getPath:@"contacts"
         parameters:@{@"accessID":self.accessId, @"name":contactName}
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 //Success code
         } 
            failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 //Error code
         }];

请注意如何将其分配给变量。此外,在使用这些路径方法时,AFHTTPClient我们不需要包含baseURL,只需要包含我们想要附加到它的路径部分。

于 2013-04-14T20:27:31.467 回答