ilo*_*acs 6 function parameter-passing swift
我正在尝试编写一个函数,可以将任何函数作为参数并在Swift中执行它.我试过这种方法:
public func anyFunc<P, T> (_ function: (P...) -> T) {
_ = function()
}
Run Code Online (Sandbox Code Playgroud)
然后尝试:
anyFunc(print("hello"))
Run Code Online (Sandbox Code Playgroud)
这产生了 ERROR: 'print' produces '()', not the expected contextual result type '(_...) -> _'
我怎样才能做到这一点(并且可行)?
如何使用@autoclosure,如下:
func anyFunc<T>(_ closure: @autoclosure () -> T) {
let result: T = closure()
// TODO: Do something with result?
}
anyFunc(print("hello"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1478 次 |
| 最近记录: |