未知的接收者' AFHTTPRequestOperation' ;你的意思是' AFHTTPRequestSerializer'

时间:2016-09-06 06:29:53

标签: ios objective-c afnetworking afnetworking-2 afnetworking-3

我正在学习AFNetworking套件。我跟着这个tutorial。现在我坚持评论2,见AFHTTPRequestOperation下面的代码。

我使用cocoapods导入AFNetworking

这是我的代码:

- (IBAction)jsonTapped:(id)sender{
    // comment 1
    NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // comment 2
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

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

    // comment 3
    self.weather = (NSDictionary *)responseObject;
    self.title = @"JSON Retrieved";
    [self.tableView reloadData];

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

        // comment 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alertView show];
    }];

    // comment 5
    [operation start];
}

现在在编译时遇到错误,请参阅屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用AFHTTPRequestOperationManager

- (void)apiRequestForPOSTWithServiceName:(NSString *)serviceName andParameters:(NSDictionary *)parameter withCompletion:(void(^)(id response, BOOL isSuccess))completion
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *url = [NSString stringWithFormat:@"%@%@", BASE_URL, serviceName];
    [manager POST:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSLog(@"JSON: %@", responseObject);

        completion(responseObject,YES);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

        completion(error,NO);
    }];
}

快乐的编码...... !!

答案 1 :(得分:0)

在导入任何AFNetworking课程之前,请确保在项目中正确包含AFNetworking。可能你没有正确导入套件。

请勿使用已弃用的UIAlertView iOS 9使用UIAlertController请参阅下面的代码。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"text mssg" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
    // Ok action code here 
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
    // Other action code here 
}];

[alert addAction:okAction];
[alert addAction:otherAction];
[self presentViewController:alert animated:YES completion:nil];