ios - Swizzling UIImage.init(名为 :) returns nil when getting the instance method

标签 ios objective-c swift method-swizzling

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

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

最佳答案

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

+[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:)))

关于ios - Swizzling UIImage.init(名为 :) returns nil when getting the instance method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024197/

相关文章:

ios - 如何在具有索引行单元格的同一 View Controller 中加载两个 TableView ?

ios - 如何将 NSExtension 和 UNNotificationExtensionCategory 键添加到 Info.plist?

ios - 单击删除按钮,Swift,删除/删除页面 View Controller 内显示的 Controller

ios - 带有 JSON 主体的 POST 请求 AFNetworking 2.0

objective-c - NSTableView 支持增量搜索吗?

javascript - 按钮可见性属性在 Kony 的 iOS 上不起作用

ios - 最大值是多少。 iOS 上的 double/float 的值?

ios - Firebase 使用电子邮件和密码创建用户给我字符串必须解包错误

swift - 如何在应用程序处于后台或在quickblox中终止时实现CallKit?

ios - UIPageViewController 不随 setViewControllers 一起前进