Search code examples
objective-cswiftclosuresobjective-c-blocks

I'm having trouble with my attempt of objective-c block equivalent in swift


Here is the objective-c code:

options.onPan = ^(MDCPanState *state){
    if (state.thresholdRatio == 1.f && state.direction == MDCSwipeDirectionLeft) {
        NSLog(@"Let go now to delete the photo!");
    }
};

Swift:

   var options = MDCSwipeToChooseViewOptions()
        options.delegate = self
        options.likedText = "Keep"
        options.likedColor = UIColor.blueColor()
        options.nopeText = "Delete"

        options.onPan = { (state: MDCPanState) in
            if state.thresholdRatio == 1.0 && state.direction == MDCSwipeDirection.Left {
                println("Let go now to delete the photo!");
            }
        }

This is throwing an error:

'(MDCPanState) -> (MDCPanState) -> $T2' is not convertible to 'MDCPanState'

Would appreciate some help thanks.


Solution

  • I'm not 100% since I don't have XCode near me at the moment, but I believe you need to change this:

    options.onPan = { (state: MDCPanState) in
            if state.thresholdRatio == 1.0 && state.direction == MDCSwipeDirection.Left {
                println("Let go now to delete the photo!");
            }
    

    to this:

    options.onPan = { (state: MDCPanState!) -> Void in
            if state.thresholdRatio == 1.0 && state.direction == MDCSwipeDirection.Left {
                println("Let go now to delete the photo!");
            }