获取实例方法时Swizzling UIImage.init(named:) returns nil

Swizzling UIImage.init(named:) returns nil when getting the instance method

我正在尝试调配 UIImage 初始化函数,但在尝试获取实例函数时,它们 return 为零。任何的想法?

let instance = class_getInstanceMethod(self, #selector(UIImage.init(named))
let instanceWithBundle = class_getInstanceMethod(self, #selector(UIImage.init(named:in:with)

它 returns 没有因为在 Objective-C 它们实际上是 class 方法:

+[UIImage imageNamed:]

+[UIImage imageNamed:inBundle:withConfiguration:]

改为使用 class_getClassMethod(并确保在方法选择器中的 namedwith 之后添加冒号):

let imageNamedMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:)))
let imageNamedInWithMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:in:with:)))