简体   繁体   English

快速使用带有参数的预定义功能作为完成块

[英]Using predefined function with parameters as completion block in swift

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. 编辑:我认为问题是Swift语法,但问题是我不知道UIViewController.presentViewController为不带输入参数的完成处理程序指定了签名。

I have to provide a completion handler to presentViewController. 我必须为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: 但是,如果像这样声明myHandler,那么上面的行会是什么样子:

func myHandler(myParameter: AnyObject){
    ...
}

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. 在问这个问题之前,我应该已经看过presentViewController声明 ,但是我对闭包没有经验,并认为可以传递任何闭包。 It turns out that the issue is that presentViewController takes a very specific closure of the form 事实证明,问题在于presentViewController采用了非常具体的形式关闭

(() -> 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. 因此,答案是presentViewController无法采用本身带有另一个参数的完成处理程序。

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 当然-> Void会根据您是否想要该功能而变化

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 . 它期望一个没有任何参数且没有返回类型/ void的闭包。 You are providing a function that expects a AnyObject and returns nothing / void . 您正在提供一个期望AnyObject并且不返回/ void的函数。 Therefore there is a mismatch between the expected (() -> Void) and the provided ((AnyObject) -> Void) 因此,预期的(() -> Void)与提供的((AnyObject) -> Void)之间不匹配

How would the program know what to pass in to myHandler as myParameter ? 程序如何知道什么传递给myHandler作为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 . 您提供完成块调用这样myHandler但实际上提供了价值myParameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM