【问题标题】:iOS Incompatible block pointer types issueiOS 不兼容的块指针类型问题
【发布时间】:2012-04-22 00:09:55
【问题描述】:

我在使用MKStoreKit 的项目中遇到了实施问题。我正在尝试使用各种购买选项实现UIAlertView

这是我做各种事情并调用UIAlertView的代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{    
    if(FALSE == payWallFlag)
    {        
        // Display Alert Dialog
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Subscription Options"
                                                          message:@"You do not have an active subscription. Please purchase one of the options below."
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                                otherButtonTitles:nil];

        [message addButtonWithTitle:@"7 Day Subscription $0.99"];

        [message show];

        return FALSE;
    } else if(TRUE == payWallFlag)
    {
        // Load content
    }
}

这是物理的alertView,带有我要调用的代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Cancel"])
    {
        NSLog(@"Cancel Button was selected.");
    }
    else if([title isEqualToString:@"7 Day Subscription $0.99"])
    {
        NSLog(@"7 Day Subscription button pressed.");
        //Buy a 7 day subscription
        if([SKPaymentQueue canMakePayments]) {
            [[MKStoreManager sharedManager] buyFeature:kFeatureAId onComplete:^(NSString* purchasedFeature)
             {
                 NSLog(@"Purchased: %@", purchasedFeature);
                 // Send an alert to the user
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Successful"
                                                                 message:@"Thank you. You have successfully purchased a 7 Day Subscription."
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil];
                 [alert autorelease];
                 [alert show];

                 // Show the user the content now
                 payWallFlag = TRUE;
                 return TRUE;
             }
                                           onCancelled:^
             {
                 // Send an alert to the user
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Failed"
                                                                 message:@"Unfortunately you have cancelled your purchase of a 7 Day Subscription. Please try again."
                                                                delegate:nil
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:nil];
                 [alert autorelease];
                 [alert show];

                 // Block the content again
                 payWallFlag = FALSE;
             }];
        }
        else 
        {
            NSLog(@"Parental control enabled");
            // Send an alert to the user
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Failed"
                                                            message:@"Unfortunately Parental Controls are preventing you from purchasing a subscription. Please try again."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert autorelease];
            [alert show];

            // Block the content again
            payWallFlag = FALSE;
        }
    }
}

问题是我在UIAlertView 中收到以下 Xcode 错误消息:

不兼容的块指针类型将“int (^)(NSString *)”发送到“void (^)(NSString *)”类型的参数

问题似乎是:onComplete:^(NSString* purchasedFeature)onCancelled:^,但我不知道如何解决这个问题。

【问题讨论】:

    标签: ios objective-c in-app-purchase uialertview mkstorekit


    【解决方案1】:

    您不应该在该块中使用return TRUE;,因为编译器假定该块返回int,而它应该返回void(因此块类型不兼容)。

    ...onComplete:^(NSString* purchasedFeature) {
      NSLog(@"Purchased: %@", purchasedFeature);
      // Send an alert to the user
      UIAlertView *alert = [[UIAlertView alloc] ...];
      [alert autorelease];
      [alert show];
    
      // Show the user the content now
      payWallFlag = TRUE;
      return TRUE; // <--- Remove this line.
    }...
    

    对于第二个块(onCancelled 之一),您可能错过了NSString* 参数,或者它所期望的任何参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      相关资源
      最近更新 更多