2

I am trying to swizzle UIImage init functions but when trying to get the instance function they return nil. Any idea?

let instance = class_getInstanceMethod(self, #selector(UIImage.init(named))
let instanceWithBundle = class_getInstanceMethod(self, #selector(UIImage.init(named:in:with)
Tal Zion
  • 5,834
  • 3
  • 45
  • 62

1 Answers1

4

It returns nil because in Objective-C they are actually class methods:

+[UIImage imageNamed:]

+[UIImage imageNamed:inBundle:withConfiguration:]

Use class_getClassMethod instead (and make sure to add colons after named and with in your method selectors):

let imageNamedMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:)))
let imageNamedInWithMethod = class_getClassMethod(UIImage.self, #selector(UIImage.init(named:in:with:)))
TylerP
  • 8,984
  • 4
  • 34
  • 41