1

I have decelerated this blocks in my objective-c code:

typedef void(^ActionStringDoneBlock)(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue);
typedef void(^ActionStringCancelBlock)(ActionSheetStringPicker *picker);

I decelerate an instance of this blocks in objective-c like below:

ActionStringDoneBlock done = ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
    selectedVisa = (int) selectedIndex;
    if ([visaView.textField respondsToSelector:@selector(setText:)]) {
        [visaView.textField performSelector:@selector(setText:) withObject:selectedValue];
    }
};

and use this instance like below:

    [ActionSheetStringPicker showPickerWithTitle:"myTitle"
                                        rows:visaData
                            initialSelection:initialSelection
                                   doneBlock:done
                                 cancelBlock:cancel
                                      origin:visaView.textField
 ];

My project users both swift and objective-c code. Now I want to use these code in a new ViewController in my swift code. I use below code:

let done = {(picker: ActionSheetStringPicker?, selectedIndex:Int, selectedValue: Any?)  in

    //My Codes

    }

    let cancel  = {
        (_ picker: ActionSheetStringPicker) -> Void in

    }

    ActionSheetStringPicker.show(withTitle: "My Title",
                                 rows: messageTitleData,
                                 initialSelection: initialSelection,
                                 doneBlock: done as ActionStringDoneBlock,
                                 cancel: cancel as! ActionStringCancelBlock,
                                 origin: messageTitle.textField
    )

but I get below error in swift code:

EXC_BREAKPOINT

I had printed the out put of done as ActionStringDoneBlock to the console an I see below result:

error: :3:1: error: cannot convert value of type '() -> ()' to type 'ActionStringDoneBlock' (aka '(Optional, Int, Optional) -> ()') in coercion

I also tried defining done as below:

  let done = {(picker: Optional<ActionSheetStringPicker>, selectedIndex:Int, selectedValue: Optional<Any>)  in

    //My Codes

    }

but again got same error. Does someone have any idea about whats the problem in the swift code?

1 Answer 1

5

You need to annotate the closure types and omit the passed types

let done : ActionStringDoneBlock  = { (picker, selectedIndex, selectedValue)  in ... }

let cancel : ActionStringCancelBlock = { picker in ... }

Without an annotation a closure is treated as () -> (). That's what the error message says.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.