Objective-C 和 SEL/IMP 的使用

标签 objective-c

我的另一个问题是关于 optimizing Objective C programs启发如下:当方法有两个(或更多)整数输入时,有人有使用 SEL 和 IMP 的简短示例吗?

最佳答案

这是一个 good tutorial用于获取当前 IMP(带有 IMP 概述)。 IMP 和 SEL 的一个非常基本的例子是:

- (void)methodWithInt:(int)firstInt andInt:(int)secondInt { NSLog(@"%d", firstInt + secondInt); }

SEL theSelector = @selector(methodWithInt:andInt:);
IMP theImplementation = [self methodForSelector:theSelector]; 
//note that if the method doesn't return void, you have to explicitly typecast the IMP, e.g. int(* foo)(id, SEL, int, int) = ...

然后您可以像这样调用 IMP:

theImplementation(self, theSelector, 3, 5);

通常没有理由需要 IMP,除非你正在做严肃的巫术——你有什么特别想做的吗?

关于Objective-C 和 SEL/IMP 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2650190/

相关文章:

objective-c - 如何检测 NSWindow 最大化或缩放事件?

objective-c - 当新的推送通知到达时重置服务器端徽章计数器

objective-c - 如何创建符合 Swift 和 Objective-C 之间共享协议(protocol)的类方法?

objective-c - iOS 表格 View 中有很多单元格

ios - UITable 滚动时自动将 UI 元素添加到 UIStackView

objective-c - 无法将类型 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.Double>' (0x11b1b84f8) 的值转换为 'NSMutableDictionary'

ios - 加载 View Controller 后如何使用计时器设置进度条

objective-c - 如何创建指向结构的指针并对其进行类型转换?

swift - App Store 提交显示 Xcode 11 中的应用程序图标问题

iphone - 如何在子项目的主项目中使用标题?