我可以使用默认参数输入一个函数吗?

时间:2018-04-08 08:06:31

标签: swift swift4

我可以输入参数有默认值的函数吗? 怎么样?

在以下示例中,由于错误而无效,我有:

  • executeCommands,一个执行命令列表的函数。此函数可以选择接收另一个函数作为参数 - 一个用于获取要执行的命令的函数。
  • fetchCommandsFromDisk,一个从磁盘中获取命令的默认函数。
  • Executioner.executeCommands(),一个程序入口点。

    /* An attempt to typealias:Executioner\fetchCommandsFromDisk( (String)->[String] ) */
    typealias commandFetcherType = (String) -> [String]
    
    class Executioner {
        /* The function whose signature is to have a typealias. */
        func fetchCommandsFromDisk(fromFile file: String = "foobar") -> [String] {
            ...
        }
    
        /* A function which if called without arguments,
         * will fall back to a default argument which is
         * the above function. */
        static public func executeCommands(commandFetcher = fetchCommandsFromDisk) {
            /* This does not work, xcode reports:
               "ERROR: Missing argument for parameter #1 in call" */
            let commands = commandFetcher()
            ... /* execute commands */ ...
        }
    }
    
    ...
    
    /*
     * Calling executeCommands without parameters.
     * This results in commands being fetched from disk, then executed.
     */
    Executioner.executeCommands()
    

2 个答案:

答案 0 :(得分:2)

您的代码中存在一些问题:

1 - 为了将fetchCommandsFromDisk用作静态executeCommands方法的默认设置,您还应将fetchCommandsFromDisk声明为 static 方法,例如:

static func fetchCommandsFromDisk(fromFile file: String = "foobar") -> [String] {
    print("\(#function): file value is: \(file)")
    return [""]
}

2 - executeCommands签名声明为:

static public func executeCommands(commandFetcher = fetchCommandsFromDisk)

无效,你缺少参数名称,所以它应该是:

static public func executeCommands(parameter: @escaping commandFetcherType = fetchCommandsFromDisk)

因此整个方法就是 - 例如 - :

static public func executeCommands(parameter: @escaping commandFetcherType = fetchCommandsFromDisk) {
    let commands = parameter

    commands("testing")
}

请注意,调用parameter必须使用参数,由于某种原因,它不会识别其fetchCommandsFromDisk参数的默认值(“foobar”),而是会查看{{1 (“测试”)值。

作为回顾,整个代码将是:

parameter

<强>输出:

根据上面的代码,让我们回顾一下每种方法的输出:

--- 在不传递参数的情况下调用typealias commandFetcherType = (String) -> [String] class Executioner { static func fetchCommandsFromDisk(fromFile file: String = "foobar") -> [String] { print("\(#function): file value is: \(file)") return [""] } static public func executeCommands(parameter: @escaping commandFetcherType = fetchCommandsFromDisk) { let commands = parameter commands("testing") } }

fetchCommandsFromDisk

--- 调用Executioner.fetchCommandsFromDisk() /* fetchCommandsFromDisk(fromFile:): file value is: foobar */ 并传递参数:

fetchCommandsFromDisk

--- 在不传递函数的情况下调用Executioner.fetchCommandsFromDisk(fromFile: "hello!") /* fetchCommandsFromDisk(fromFile:): file value is: hello! */

executeCommands

请记住,您将根据Executioner.executeCommands() /* fetchCommandsFromDisk(fromFile:): file value is: testing */ 实施中传递的内容进行“测试”,但默认值为executeCommands

--- 调用fetchCommandsFromDisk并传递-function-参数:

executeCommands

答案 1 :(得分:0)

看来你在Xcode / swift中发现了一个错误?为什么不说这是一种元组类型?

enter image description here

和......

enter image description here