当前位置:网站首页>swift 代码实现方法调用

swift 代码实现方法调用

2022-06-26 08:10:00 may_he

class X {
  @objc func sayHiTo(name: String) {
    print("Hello \(name)!")
  }
}

let obj = X()
let sel = #selector(obj.sayHiTo)
let method = class_getInstanceMethod(object_getClass(obj), sel)
let imp = method_getImplementation(method)

typealias ClosureType = @convention(c) (AnyObject, Selector, String) -> Void
let sayHiTo : ClosureType = unsafeBitCast(imp, ClosureType.self)
sayHiTo(obj, sel, "May")

 

原网站

版权声明
本文为[may_he]所创,转载请带上原文链接,感谢
https://blog.csdn.net/may_he/article/details/81262032