简体   繁体   中英

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.

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!");
        }

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