子类中调用父类的私有方法

对于在使用cocoapods中,可能三方库中的类并不能满足实际需求,因此我们要对这些类进行子类化或者category等操作。假设我们在子类中想调用父类的方法,对于父类的.m文件内容可见的情况即已经知道具体想调用的方法名时,可以:

第一种最简单的办法

if ([super respondsToSelector:@selector(methodName)]) {

[super performSelector:@selector(methodName)];

}

第二种可以通过runtime获取父类的该方法

Method oriM = class_getInstanceMethod([super class], @selector(methodName));

SEL selector = method_getName(oriM);

[super respondsToSelector:selector];

如会出现“undeclared selector”的warning时

1.可在当前文件文件或.pch中导入

#pragma GCC diagnostic ignored "-Wundeclared-selector"

2.如下在警告的代码前后加上

#pragmaclang diagnostic push
#pragmaclang diagnostic ignored"-Warc-performSelector-leaks"

warning code...

#pragmaclang diagnostic pop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.