简体   繁体   中英

Converting Swift block to Objective-C block

In Swift, I have created a closure method (I think):

func firstMove(action: UIAlertAction!) {
        if action.title == "Yes" {
            currentPlayer = "X"
        } else {
            currentPlayer = "0"
        }

That I pass into this UIAlertAction method:

let optionToStartController = UIAlertController(title: "", message: "Do you want first move?", preferredStyle: .Alert)
            optionToStartController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: firstMove))

How can I convert both the closure and method to Objective-C?

I have tried doing:

- (void)firstMove:(UIAlertAction*)action
{
  if ([action.title isEqual: @"Yes"]) {
    _currentPlayer = 'X';
} else {
    _currentPlayer = 'O';
 }
}

And passing it like this:

UIAlertController *optionToStartController = [UIAlertController alertControllerWithTitle:@"" message:@"Do you want first move?" preferredStyle:UIAlertControllerStyleAlert];
[optionToStartController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler: firstMove]];

You may be looking for blocks, the objective-C rough equivalent of closures. I'm not quite sure what you're trying to accomplish, but the following code defines block firstMove and how it is passed and called from method addAction.

void(^firstMove)(int) = ^(int x){
    NSLog(@"print y: %d", x);
};

[self addAction:firstMove];

...

-(void)addAction:(void(^)(int))y{
    y(5);
}

You can see the example. It should work properly.

UIAlertController * alert=   [UIAlertController
                                 alertControllerWithTitle:@"Question"
                                 message:@"Do you want first move?"
                                 preferredStyle:UIAlertControllerStyleAlert];
    //First button
   UIAlertAction* ok = [UIAlertAction
                        actionWithTitle:@"YES"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {
//Actions if the user press this button
//Dismiss the alertController. It's preferred to be in all actions.
                            [alert dismissViewControllerAnimated:YES completion:nil];
_currentPlayer = @"X";

                        }];

//Second Button
   UIAlertAction* cancel = [UIAlertAction
                            actionWithTitle:@"NO"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
//Actions if the user press this button
//Dismiss the alertController. It's preferred to be in all actions.
                               [alert dismissViewControllerAnimated:YES completion:nil];
_currentPlayer = @"O";

                           }];
    //Add the actions to the alertController
   [alert addAction:ok];
   [alert addAction:cancel];


//present the alertController on the device. If you are writing this in the viewController, you can use self.view, if you are in UIView, then just self
   [self.view presentViewController:alert animated:YES completion:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM