一、先了解下runtime
RunTime简称运行时。就是系统在运行的时候的一些机制,其中最主要的是消息机制。对于C语言,函数的调用在编译的时候会决定调用哪个函数。编译完成之后直接顺序执行,无任何二义性。OC的函数调用称为消息发送。属于动态调用过程。在编译的时候并不能决定真正调用哪个函数(事实证明,在编 译阶段,OC可以调用任何函数,即使这个函数并未实现,只要申明过就不会报错。而C语言在编译阶段就会报错)。只有在真正运行的时候才会根据函数的名称找 到对应的函数来调用。
在OC中有些文件只给出了声明的头文件,里面有一些可以被我们调用的函数和属性,而头文件中没有声明的我们便望尘莫及了,不可能被我们调用到。而根据runtime的特点,我们可以看到这些私有方法和属性,并且调用。(虽然调用私有方法的app上架会被reject,但是仍然阻挡不住我们的脚步)
//Calculator.h
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
- (NSNumber *)sumAddend1:(NSNumber *)adder1 addend2:(NSNumber *)adder2;
- (NSNumber *)sumAddend1:(NSNumber *)adder1 :(NSNumber *)adder2;
@end
//Calculator.m
#import <objc/runtime.h>
#import "Calculator.h"
@implementation Calculator
- (NSNumber *)sumAddend1:(NSNumber *)adder1 addend2:(NSNumber *)adder2{
NSLog(@"Invorking method on %@ object with selector %@",[self class],NSStringFromSelector(_cmd));
return [NSNumber numberWithInteger:([adder1 integerValue] +
[adder2 integerValue])];
}
- (NSNumber *)sumAddend1:(NSNumber *)adder1 :(NSNumber *)adder2{
NSLog(@"Invorking method on %@ object with selector %@",[self class],NSStringFromSelector(_cmd));
return [NSNumber numberWithInteger:([adder1 integerValue] +
[adder2 integerValue])];
}
@end
//main文件
#import <Foundation/Foundation.h>
#import "Calculator.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 创建类的一个实例(对象)
Calculator *calc = [Calculator new];
NSNumber *addend1 = [NSNumber numberWithInteger:25];
NSNumber *addend2 = [NSNumber numberWithInteger:10];
NSNumber *addend3 = [NSNumber numberWithInteger:15];
// 选择器使用@selector()指令,在编译时创建的
SEL selector1 = @selector(sumAddend1:addend2:);
id sum1 = [calc performSelector:selector1 withObject:addend1 withObject:addend2];
NSLog(@"Sum of %@ + %@ = %@",addend1,addend2,sum1);
// 选择器使用NSSelectorFromString()函数,在程序运行时创建的
SEL selector2 = NSSelectorFromString(@"sumAddend1::");
id sum2 = [calc performSelector:selector2 withObject:addend1 withObject:addend3];
NSLog(@"Sum of %@ + %@ = %@",addend1,addend3,sum2);
}
return 0;
}
运行结果(ps:需要引入头文件:<objc/runtime.h>,才可以调用runtime里面的函数):
2016-11-18 21:10:55.147 7.2.3.****使用对象消息****[1059:46233] Invorking method on Calculator object with selector sumAddend1:addend2:
2016-11-18 21:10:55.148 7.2.3.****使用对象消息****[1059:46233] Sum of 25 + 10 = 35
2016-11-18 21:10:55.148 7.2.3.****使用对象消息****[1059:46233] Invorking method on Calculator object with selector sumAddend1::
2016-11-18 21:10:55.148 7.2.3.****使用对象消息****[1059:46233] Sum of 25 + 15 = 40
Program ended with exit code: 0
可以看到,通过runtime机制,在程序运行时才决定去调用哪个函数。在main中,使用运行时调用了Calculator类中的两个对象方法。并且这两个方法还在头文件中声明了,一定会被外界调用到。但是运行时机制可以在运行时去决定调用哪一个函数,那么它一定能知道类中有哪些方法,无论是私有的还是公有的。
根据这一点,把Calculator类头文件中声明的函数注释掉,在程序运行时应该还是能调用到这两个方法。
//Calculator.h
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
//- (NSNumber *)sumAddend1:(NSNumber *)adder1 addend2:(NSNumber *)adder2;
//- (NSNumber *)sumAddend1:(NSNumber *)adder1 :(NSNumber *)adder2;
@end
运行:
编译没有报错,并且得到的结果和之前的是一模一样的。证明猜想正确。
二、调用私有方法
现在来试试调用私有方法:
首先拿UIView下手:
先来看看这个类下面所有的函数
//先获取类名
NSString *className = NSStringFromClass([UIView class]);
const char *cClassName = [className UTF8String];
id theClass = objc_getClass(cClassName);
//用来计数
unsigned int outCount;
Method *m = class_copyMethodList(theClass,&outCount);
NSLog(@"%d",outCount);
for (int i = 0; i<outCount; i++) {
SEL a = method_getName(*(m+i));
NSString *sn = NSStringFromSelector(a);
NSLog(@"%@",sn);
}
结果:
**206-11-18 21:24:22.402 JavaScriptCore-Demo[1088:52065] 1278**
**2016-11-18 21:24:22.402 JavaScriptCore-Demo[1088:52065] _accessibilityCirclePathBasedOnBoundsWidth**
**2016-11-18 21:24:22.403 JavaScriptCore-Demo[1088:52065] _accessibilityOnlyComparesByXAxis**
**2016-11-18 21:24:22.403 JavaScriptCore-Demo[1088:52065] _accessibilitySortedElementsWithin**
**2016-11-18 21:24:22.403 JavaScriptCore-Demo[1088:52065] _accessibilityViewController**
**2016-11-18 21:24:22.403 JavaScriptCore-Demo[1088:52065] _accessibilityVerticalSizeClass**
**2016-11-18 21:24:22.403 JavaScriptCore-Demo[1088:52065] _accessibilityHorizontalSizeClass**
**2016-11-18 21:24:22.404 JavaScriptCore-Demo[1088:52065] _accessibilityAutomaticIdentifier**
**2016-11-18 21:24:22.404 JavaScriptCore-Demo[1088:52065] _accessibilitySupportGesturesAttributes**
**2016-11-18 21:24:22.404 JavaScriptCore-Demo[1088:52065] _accessibilityIsUserInteractionEnabled**
**2016-11-18 21:24:22.404 JavaScriptCore-Demo[1088:52065] _accessibilityServesAsContainingParentForOrdering**
**2016-11-18 21:24:22.404 JavaScriptCore-Demo[1088:52065] _accessibilityUserTestingChildren**
**2016-11-18 21:24:22.405 JavaScriptCore-Demo[1088:52065] _accessibilityObscuredScreenAllowedViews**
**2016-11-18 21:24:22.405 JavaScriptCore-Demo[1088:52065] accessibilityIsWindow**
**2016-11-18 21:24:22.410 JavaScriptCore-Demo[1088:52065] _accessibilityHitTest:withEvent:**
**2016-11-18 21:24:22.410 JavaScriptCore-Demo[1088:52065] _axResponderChain**
**2016-11-18 21:24:22.411 JavaScriptCore-Demo[1088:52065] _accessibilityChildVendingParent**
**2016-11-18 21:24:22.411 JavaScriptCore-Demo[1088:52065] _superAccessibilityHitTest:withEvent:**
**2016-11-18 21:24:22.411 JavaScriptCore-Demo[1088:52065] _accessibilityPostNotification:**
**2016-11-18 21:24:22.411 JavaScriptCore-Demo[1088:52065] _axWindowSubviews**
**2016-11-18 21:24:22.411 JavaScriptCore-Demo[1088:52065] _axPrintSubviews:string:**
**2016-11-18 21:24:22.412 JavaScriptCore-Demo[1088:52065] _accessibilityShouldHitTestLayers**
**2016-11-18 21:24:22.412 JavaScriptCore-Demo[1088:52065] _accessibilityCheckForAllowedModalView:event:**
**2016-11-18 21:24:22.412 JavaScriptCore-Demo[1088:52065] _accessibilityMaxFuzzyHitTestDistance**
**2016-11-18 21:24:22.412 JavaScriptCore-Demo[1088:52065] _accessibilityModalViewBlocksView:blockerView:**
UIView类中总共有1278个方法,我这只展示了一部分,大家可以亲自试一试。
在这些方法中我挑选了一个setMaskView:方法,想调用下这个私有方法。
Snip20161118_1.png这是我没有调用前的界面
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
maskView.backgroundColor = [UIColor redColor];
SEL selector = NSSelectorFromString(@"setMaskView:");
[self.view performSelector:selector withObject:maskView];
Snip20161118_2.png
这是我调用后的界面。
到此,我们就成功的调用了私有方法。令我疑惑的是,我给maskView的框架是(0, 0, 200, 200),背景颜色是红色,最后显示出来的效果是这个样子,可能是我理解上还存在一些问题,希望大家能够指正批评。
网友评论