Swift 将静态方法的引用传递给函数

Swift 将静态方法的引用传递给函数,swift,function,parameter-passing,static-methods,Swift,Function,Parameter Passing,Static Methods,我在Swift中发现,您可以存储静态方法的引用,而无需实际执行它: class StringMama { static func returnString(s:String)->String { return s } } var stored = StringMama.returnString print(stored.self) // (Function) print(stored(s:"Hello!")) // for some reason it doe

我在
Swift
中发现,您可以存储静态方法的引用,而无需实际执行它:

class StringMama {
    static func returnString(s:String)->String {
      return s
    }
}

var stored = StringMama.returnString
print(stored.self) // (Function)
print(stored(s:"Hello!")) // for some reason it doesn't work
print(stored("Hello!")) // it works

现在,我想将
存储的
作为函数的参数传递给以后在函数体中执行函数。这可能吗?怎么用?我找不到办法。非常感谢您的帮助

您可以这样做:

    printUsingReference(stored, "the")


static func printUsingReference(_ stored: (_ s: String) -> String, _ content: String) {
    print(stored(content))

}
func Foo(param: (String) -> String) {
    print(param("Foo called"))
}

// Pass it your stored var
Foo(param: stored)

你为什么不使用闭包?存储它对变量的引用,并从中获取值,然后传入函数

var someValue: (String) -> String = { stringValue in
  return stringValue
}

func someFunction(value: String) {
 print(value)
}

//Now pass closure reference into one variable 
let value = someValue
//Using in that function like below.
someFunction(value: value("test"))

奇怪的是,我也尝试过同样的方法,但没有成功,后来我尝试了你的代码,结果成功了(可能是游乐场的缓存问题),但在表达式上使用postfix
.self
只会返回该表达式;因此,您可以只说
print(stored)
,而不说
print(stored.self)