如何在swift中使用函数成员指针?

How to use a function member pointer in swift?

我正在尝试为操作重新分配一个新值 "doo" 但我收到错误 "Cannot assign value of type '() -> ()' to type '(myClass) -> () -> ()'" 我该如何解决?

class myClass {

    var action = foo    //function pointer

    var actions = [foo, doo] //function pointer array

    var i = 12


    func foo() {
        print("NOP: \(i)")
    }

    func doo() {
        print("doo: \(i)")
    }

    func change()
    {

        action = actions[1] //this compiles

        //Cannot assign value of type '() -> ()' to type '(myClass) -> () -> ()'
        action = doo    //doesn't compile :(


        //... later:
        action(self)() //call the function pointer
    }

}

替换这个

action = doo  

action = type(of:self).doo // or myClass.doo

问题出在函数内部 change 您引用 doo 函数是关于 self 而不是类型 myClass 本身