Yam*_*man 6 function objective-c dynamictype swift
我试图得到一个像这样的函数的引用:
class Toto {
func toto() { println("f1") }
func toto(aString: String) { println("f2") }
}
var aToto: Toto = Toto()
var f1 = aToto.dynamicType.toto
Run Code Online (Sandbox Code Playgroud)
我有以下错误: Ambiguous use of toto
如何获取具有指定参数的函数?
Mar*_*n R 12
由于Toto有两个具有相同名称但签名不同的方法,您必须指定所需的方法:
let f1 = aToto.toto as () -> Void
let f2 = aToto.toto as (String) -> Void
f1() // Output: f1
f2("foo") // Output: f2
Run Code Online (Sandbox Code Playgroud)
或者(正如@Antonio正确指出的那样):
let f1: () -> Void = aToto.toto
let f2: String -> Void = aToto.toto
Run Code Online (Sandbox Code Playgroud)
如果您需要将该类的实例作为第一个参数的curried函数,那么您可以以相同的方式继续,只有签名是不同的(比较@Antonios评论到您的问题):
let cf1: Toto -> () -> Void = aToto.dynamicType.toto
let cf2: Toto -> (String) -> Void = aToto.dynamicType.toto
cf1(aToto)() // Output: f1
cf2(aToto)("bar") // Output: f2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1117 次 |
| 最近记录: |