AFNetworking NSData - 不兼容的块指针类型发送

时间:2015-04-08 01:18:55

标签: ios objective-c afnetworking nsdata

我正在尝试重做一些代码来使用AFNetworking。我有以下方法:

-(NSArray *)GetTableDataOfPhase:(NSString *)phase
{
    NSString *phaseRequestString = [NSString stringWithFormat:@"%@?jobNo=%@",kIP,phase];
    NSURL *JSONURL = [NSURL URLWithString:phaseRequestString];
    NSURLResponse* response = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
            NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        if(data == nil)
            return nil;
        NSError *myError;
        NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];



    return tableArray;
}

现在我正在尝试改变它,所以它仍然返回一个数组,我试过这样做:

-(NSArray *)GetTableDataOfPhase:(NSString *)phase
{
    NSString *phaseRequestString = [NSString stringWithFormat:@"%@?jobNo=%@",kIP,phase];
    NSURL *JSONURL = [NSURL URLWithString:phaseRequestString];
    NSURLResponse* response = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];

    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

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

        NSData* data = [NSURLConnection sendSynchronousRequest:responseObject returningResponse:&response error:nil];
        if(data == nil)
            return nil;
        NSError *myError;
        NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];

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


    }];

    [operation start];


    return tableArray;
}

但是我收到了这个错误:

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

和这个警告:

/Users/jamessuske/Documents/My Programs/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:170:97: Sending 'NSURLResponse *const *' to parameter of type 'NSURLResponse **' discards qualifiers

这就是我所说的:

- (void)GetRequest
{
    //refresh table view
    [dataSource.editedCellHolder removeAllObjects];
    [dataSource.cellHolder removeAllObjects];
    [dataSource.cellHolderDisplay removeAllObjects];


    NSArray *tableData = [dataSource.areaData GetTableDataOfPhase:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];

    if(tableData == nil)
        [self CustomAlert:@"Data was not recieved from the server, please check internet/VPN settings, Or contact Software Vendor for assistance."];
    [dataSource PopulateTableData:tableData];
    [indicatorView stopAnimating];
    [indicatorView removeFromSuperview];
    [loadingView removeFromSuperview];
    loadingView = nil;
    indicatorView =nil;
    [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(DisplayTable) userInfo:nil repeats:NO];

}

1 个答案:

答案 0 :(得分:1)

有几件事:

  1. 使用AFNetworking,您应该完全丢失NSURLConnection请求。

  2. 同样,默认的responseSerializer会为您执行JSON解析,因此您可能会丢失NSJSONSerialization解析。 AFNetworking为您完成所有这些工作。

  3. 同样,不要手动构建URL参数,而是让AFNetworking为您做到这一点。默认情况下,AFNetworking使用requestSerializer来为您构建请求。

  4. 您的旧方法同步运行,这通常是一个坏主意。相反,您应该使用异步模式(例如completionHandler块)。

  5. 所以,将所有这些结合起来,可能看起来像:

    - (void)getTableDataOfPhase:(NSString *)phase completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler
    {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = @{@"jobNo" : phase};
    
        [manager GET:kIP parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            completionHandler(responseObject, nil);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            completionHandler(nil, error);
        }];
    }
    

    你会这样称呼它:

    [self getTableDataOfPhase:@"..." completionHandler:^(NSArray *resultsObject, NSError *error) {
        if (resultsObject) {
            // use NSArray here
        } else {
            NSLog(@"error = %@", error);
        }
    }];
    
    // but don't try to use the `resultsObject` array here!