Silviu St
Silviu St

Reputation: 1842

Converting syntax from Objective C to Swift ^()

I have a Swift project in which I am using a Objective C Custom Gesture Recognizer.

I have converted with success everything untill now. Can't quite figure how to convert that return ^(UITableView *tableView, NSIndexPath *indexPath) { }; Everything I've tried didn't compile.

Objective C code:

 typedef void(^DRCellSlideActionBlock)(UITableView *tableView, NSIndexPath *indexPath);
    typedef void(^DRCellSlideActionStateBlock)(DRCellSlideAction *action, BOOL active);




  -(void)doSomething{
            squareAction.didTriggerBlock = [self pushTriggerBlock];
    }

- (DRCellSlideActionBlock)pushTriggerBlock {

    return ^(UITableView *tableView, NSIndexPath *indexPath) {
           NSLog(@"Do Something");
        }]];

        [self presentViewController:alertController animated:YES completion:nil];
    };
}

What I've tried:

Error: Consecutive statements must be separated....

   return (tableView: UITableView, indexPath: NSIndexPath) in {
}

Also tried this as for the completion of (UIView.animateWithDuration) :

Error: Consecutive statements must be separated....

return () -> (tableView: UITableView, indexPath: NSIndexPath) in {

By the way, I'm kinda new to Swift so excuse my code

Upvotes: 0

Views: 61

Answers (1)

Why don't you just look up closures' syntax in the official Swift guide? You are completely misled as to what closures look like.

return { (tableView: UITableView, indexPath: NSIndexPath) in
     // closure body here
}

Upvotes: 1

Related Questions