在Swift中使用外部参数名称时的方法签名是什么

Kyl*_*len 3 cocoa objective-c selector swift

我在我的Swift代码中使用了一个NSTimer对象,它需要将方法签名传递给它的'selector'参数,以便反复执行所述方法.当方法签名没有外部参数名称时

func timerMethod(internal: String) { ... }
Run Code Online (Sandbox Code Playgroud)

我可以传递这个签名的计时器对象:

var timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
        target: self,
        selector: Selector("timerMethod:"),
        userInfo: userInfo,
        repeats: true)
Run Code Online (Sandbox Code Playgroud)

但是,如果我给方法一个带有外部参数名称的签名,例如:

func timerMethod(external internal: String) { ... }
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何调用该方法.我尝试使用以下方式记录它:

println("\(__FUNCTION__)")
Run Code Online (Sandbox Code Playgroud)

其中记录以下内容:

timerMethod(external:)
Run Code Online (Sandbox Code Playgroud)

但每当我尝试这个或以下任何一个时,我都会收到'无法识别的选择器'异常:

timerMethod:
timerMethod:external
timerMethod:external:
timerMethod:(external)
timerMethod:(external:)
timerMethod(external):
Run Code Online (Sandbox Code Playgroud)

现在难倒了.有人跑到类似的东西?

Chr*_*ich 5

它是timerMethodWithExternal:你可以测试与object_getClass(t).instancesRespondToSelector(Selector("timerMethodWithExternal:"))

我使用以下代码进行内省

func with(t: Test, inout count : CUnsignedInt) -> UnsafePointer<Method> {
    var mc : CUnsignedInt = 0
    return class_copyMethodList(object_getClass(t), &count)
}

var i=0
var mc : CUnsignedInt = 0
var t = Test()
var mlist = with(t,&mc)
var n : Int = Int(mc)
for (i=0; i<n;i++) {
    println(sel_getName(method_getName(mlist[i])))
}
Run Code Online (Sandbox Code Playgroud)