第1种方法
SEL selector = NSSelectorFromString(@"setContentInsetAdjustmentBehavior:");
if ([tbView respondsToSelector:selector]) {
IMP imp = [tbView methodForSelector:selector];
void (*func)(id, SEL, NSInteger) = (void *)imp;
func(tbView, selector, 2);
// [tbView performSelector:@selector(setContentInsetAdjustmentBehavior:) withObject:@(2)];
}
第2种方法
SEL selector = NSSelectorFromString(@"setContentInsetAdjustmentBehavior:");
((void (*)(id, SEL))[tbView methodForSelector:selector])(tbView, selector);
第3种方法 忽略警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector: sel withObject: nil nil];
#pragma clang diagnostic pop
也可以定义一个宏
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
SuppressPerformSelectorLeakWarning(
[target performSelector:sel withObject:nil]
);