0

EDIT: I thought the issue was Swift syntax, but the problem was that I did not know that the UIViewController.presentViewController specifies a signature for the completion handler that takes no input parameters.

I have to provide a completion handler to presentViewController. The completion handler I want to provide takes an input parameter. What is the syntax to handle this in swift?

If the handler did not take an input parameter it would look like this:

self.presentViewController(activityVC, animated: true, completion: myHandler)

But how would the above line look if myHandler was declared like this:

func myHandler(myParameter: AnyObject){
    ...
}
6
  • dispatch_block_t maybe? developer.apple.com/library/prerelease/ios/documentation/… Commented Jun 28, 2015 at 15:10
  • are you trying to pass in a function that expects one parameter as a completion block which does not provide a parameter? Commented Jun 28, 2015 at 15:13
  • That does not look like it relates to the question, I'll try to edit to make it more clear what I am asking about Commented Jun 28, 2015 at 15:14
  • Well the self.presentViewController uses (() -> Void) as its completion handler if that is what you want. If you want it to return a value you would use something like (() -> Int) Commented Jun 28, 2015 at 15:15
  • Ok, I'm not looking to return a value, but I guess the same answer goes for input values. The signature of the expected completion handler takes no inputs, is that correct? Commented Jun 28, 2015 at 15:19

3 Answers 3

1

I should have looked at the declaration of presentViewController before asking the question, but I am inexperienced with closures and thought any closure could be passed. It turns out that the issue is that presentViewController takes a very specific closure of the form

(() -> Void)?

The full signature is here:

func presentViewController(_ viewControllerToPresent: UIViewController,
              animated flag: Bool,
            completion completion: (() -> Void)?)

So the answer is that presentViewController cannot take a completion handler that itself takes another parameter.

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

Comments

0

That does not work unfortunately. You cannot just pass in a function that has an invalid type for the completion block. It expects a closure without any parameters and with no return type / void. You are providing a function that expects a AnyObject and returns nothing / void. Therefore there is a mismatch between the expected (() -> Void) and the provided ((AnyObject) -> Void)

How would the program know what to pass in to myHandler as myParameter?

The only way I can think of to achieve this is to write a wrapper for the custom completion handler:

self.presentViewController(activityVC, animated: true, completion:{self.myHandler("")})

That way you provide a completion block that calls the myHandler but actually provides a value for myParameter.

2 Comments

You're right, taking a look at the declaration of UIViewController.presentViewController clearly shows why it can't be done. Another question is what the syntax would be if presentViewController expected a completion handler with an input parameter..
@Videre THEN it would be the same like if it would not expect one, then you would just write self.presentViewController(vc, animated: true, completion:myHandler) - iif myHandler expects a parameter of the same type and has a matching return type.
0

I believe you are looking to do the following:

function myHandler(myParameter: (() -> Void)) {
//code here
}

Of course -> Void would change depending on if you want the function

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.