2

我正在尝试调配UIImage初始化函数,但是在尝试获取实例函数时,它们返回 nil。任何想法?

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

1 回答 1

4

它返回 nil 因为在 Objective-C 中它们实际上是类方法:

+[UIImage imageNamed:]

+[UIImage imageNamed:inBundle:withConfiguration:]

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

let imageNamedMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:)))
let imageNamedInWithMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:in:with:)))
于 2020-02-02T09:14:05.930 回答