//动态调用方法
// let selector = NSSelectorFromString("searchBarShouldBeginEditing:")
// if let delegate = searchBarDelegate {
// if let method = extractMethodFrom(owner: searchBarDelegate!, selector: selector) {
// method(searchBar)
// }
// }
func extractMethodFrom(owner: AnyObject, selector: Selector) -> ((Any?) -> Any)? {
let method: Method?
if owner is AnyClass {
method = class_getClassMethod(owner as? AnyClass, selector)
} else {
print(type(of: owner))
method = class_getInstanceMethod(type(of: owner), selector)
}
if let one = method {
let implementation = method_getImplementation(one)
typealias Function = @convention(c) (AnyObject, Selector, Any?) -> Void
let function = unsafeBitCast(implementation, to: Function.self)
return { userinfo in function(owner, selector, userinfo) }
} else {
return nil
}
}
网友评论