【问题标题】:Incompatible block pointer types in不兼容的块指针类型
【发布时间】:2014-03-22 06:24:24
【问题描述】:

这是我编译时出错的方法(请参阅requestMainPage 方法)

- (void)loginToMistarWithPin:(NSString *)pin password:(NSString *)password success:(void (^)(void))successHandler failure:(void (^)(void))failureHandler {

    NSURL *url = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/Login"];

    //Create and send request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [NSString stringWithFormat:@"Pin=%@&Password=%@",
                            [self percentEscapeString:pin],
                            [self percentEscapeString:password]];
    NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:postBody];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse
*response, NSData *data, NSError *error)
     {
         // do whatever with the data...and errors
         if ([data length] > 0 && error == nil) {
             NSError *parseError;
             NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
             if (responseJSON) {
                 // the response was JSON and we successfully decoded it

                 NSLog(@"Response was = %@", responseJSON);

                 // assuming you validated that everything was successful, call the success block

                 if (successHandler)
                     successHandler();
             } else {
                 // the response was not JSON, so let's see what it was so we can diagnose the issue

                 NSString *loggedInPage = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 NSLog(@"Response was not JSON (from login), it was = %@", loggedInPage);

                 if (failureHandler)
                     failureHandler();
             }
         }
         else {
             NSLog(@"error: %@", error);

             if (failureHandler)
                 failureHandler();
         }
     }]; }

- (NSData *)requestMainPage {

    NSData *returner;
    //Now redirect to assignments page

    NSURL *homeURL = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage"];
    NSMutableURLRequest *requestHome = [[NSMutableURLRequest alloc] initWithURL:homeURL];
    [requestHome setHTTPMethod:@"GET"]; // this looks like GET request, not POST

    [NSURLConnection sendAsynchronousRequest:requestHome queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *homeResponse, NSData *homeData, NSError *homeError) //Error is in this line.
     {
         // do whatever with the data...and errors
         if ([homeData length] > 0 && homeError == nil) {
             NSError *parseError;
             NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:homeData options:0 error:&parseError];
             if (responseJSON) {
                 // the response was JSON and we successfully decoded it

                 NSLog(@"Response was = %@", responseJSON);
             } else {
                 // the response was not JSON, so let's see what it was so we can diagnose the issue

                 NSString *homePage = [[NSString alloc] initWithData:homeData encoding:NSUTF8StringEncoding];
                 NSLog(@"Response was not JSON (from home), it was = %@", homePage);
                 return homePage;
             }
         }
         else {
             NSLog(@"error: %@", homeError);
         }
         return [NSString stringWithFormat:@"%@", homeData];
     }]; }

当我决定我希望我的方法 (requestMainPage) 的返回类型为 NSData * 而不是只返回 void 时,错误出现了。
所以我不确定问题出在哪里。

【问题讨论】:

  • 您发布的代码除了中途被截断以外没有任何错误或警告。
  • 显示您对sendAsynchronousRequest 的所有呼叫,包括您的阻止。
  • 1.那么你的块在哪里? 2. 你不能那样做 ;) 因为你发送一个异步请求,requestMainPage 方法会立即返回。如果您想使用块(它们很棒),您必须在完成块中执行操作,而不是在您调用 requestMainPage 的方法中。
  • 请谨慎用词。 “抛出”的代码意味着代码编译、运行并抛出异常。看起来编译器正在抱怨代码并给出错误。比如说。要精确。你是程序员,你必须精确。由于块的声明仅以 ^ 开头并且没有返回类型,因此返回类型是从块中的代码派生的。该方法需要一个返回 void 的块,从您的错误看来,您的块返回 NSString*。发布区块会很有用;没有这个,这只是一个猜测。
  • 对不起。我已经编辑了我的帖子以包含整个方法

标签: ios objective-c compiler-errors


【解决方案1】:

我发现了我的错误,您不能从块内返回值。
因此,如果我删除 return homeErrorreturn [NSString stringWithFormat:@"%@", homeData];,问题就会消失,我可以编译

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多