【问题标题】:conflict in return type of the function + IOS函数返回类型冲突+IOS
【发布时间】:2015-04-05 02:58:12
【问题描述】:

我正在构建一个 iOS 应用程序,并且我正在使用 AFNetworking 库将数据发布到服务器。

我创建了一个函数来发布数据并在我的应用程序的不同类中调用此函数。

我在名为“responseObject”的变量中得到请求的响应,我想在返回语句中返回它我无法识别responseObject的返回类型以及函数的返回类型,我尝试使用NSDictionary但是收到以下警告:

Conflicting return type in implementation of 'postData:parameters:': 'void' vs 'NSDictionary *'

这是我的代码:

-(void)postData:(NSString*)postUrl parameters:(NSDictionary *)params{


    NSURL *baseURL = [NSURL URLWithString:@"http://xyz:2424/api/"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:postUrl parameters:params success:^(NSURLSessionDataTask *task, id responseObject)

     {

         NSLog(@"response   %@",responseObject);

     }
                failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"\n============== ERROR ====\n%@",error);

              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error adding book" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show];


          }];

}

【问题讨论】:

    标签: ios objective-c post afnetworking-2 http-method


    【解决方案1】:

    首先你可以通过键入 NSLog(@"ClassType-%@",[responseObject class]) 来查看 responseObject 的类类型。然后通过自定义委托将 responseObject 传递给所需的类型。

    【讨论】:

    • 我将类设为 __NSCFDictionary 但这是我无法在函数返回类型中返回的内容,请您提出一些建议
    • 请显示您想要返回此字典的方法。因为在代码中您没有;在nslog 之后没有显示任何工作。
    • 我不想要方法的 void 返回类型,我想要返回类型为 responseObject。并且 responseObject 有类 __NSCFDictionary
    • 好的,但是你不能返回字典,因为它是块函数。所以你可以使用 CUSTOM DELEGATE 或 NSNOTIFICATIONDEFAULTCENTER。
    【解决方案2】:

    这意味着你的 postData 方法期望返回一个 NSDictionary,但是你的方法说“void” - 看看你的头文件中的方法定义,我希望它是:

    -(NSDictionary*)postData:(NSString*)postUrl parameters:(NSDictionary *)params;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多