ios - 中不兼容的 block 指针类型

标签 ios objective-c compiler-errors

这是在编译时给我错误的方法(请参见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时出现错误。
所以我不确定问题出在哪里。

最佳答案

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

关于ios - 中不兼容的 block 指针类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22574231/

相关文章:

iOS Quickblox 聊天 : getting error: Code=-999 "You need to enable stream management to use this feature

iphone - iOS 修复 TableView 到底部,如消息应用程序

Java iOS MDM 政策

objective-c - presentModalViewController 无法在已越狱的 iOs 4.0.1 设备上运行

objective-c - 加载纹理时动画屏幕

c++ - 如何在 header 中声明后初始化二维字符数组

C++ - 使用 vector <衍生类>的静态函数找不到声明的标识符

iphone - 从主屏幕重新打开应用程序时如何刷新标签栏内容? (iOS/Xcode)

iphone - 如何在 iOS 应用程序中制作 Twitter 的行菜单?

C++ : Interix signals