您可以通过以下方式实现,您只需在中将其方法公开aProtocol
给Objective-c
运行时即可。overriding
extension
@objc protocol aProtocol { typealias completiOnBlock= (_ finished:Bool)->() func doSomething(completion: completionBlock) } extension UIView: aProtocol { func doSomething(completion: (Bool) -> ()) { print("Im an UIView") } } extension UILabel { override func doSomething(completion: (Bool) -> ()) { // you can call super.doSomething(completion: completion) print("im an UILabel") } } extension UIImageView { override func doSomething(completion: (Bool) -> ()) { // you can call super.doSomething(completion: completion) print("im an UIImageView") } }
输出:
Im an UIView Im an UIView im an UILabel im an UIImageView