-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIResponder+UIAutoTest.m
39 lines (35 loc) · 1.16 KB
/
UIResponder+UIAutoTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "UIResponder+UIAutoTest.h"
#import <objc/runtime.h>
@implementation UIResponder (UIAutoTest)
-(NSString *)nameWithInstance:(id)instance {
unsigned int numIvars = 0;
NSString *key=nil;
Ivar * ivars = class_copyIvarList([self class], &numIvars);
for(int i = 0; i < numIvars; i++) {
Ivar thisIvar = ivars[i];
const char *type = ivar_getTypeEncoding(thisIvar);
NSString *stringType = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];
if (![stringType hasPrefix:@"@"]) {
continue;
}
if ((object_getIvar(self, thisIvar) == instance)) {//此处 crash 不要慌!
key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
break;
}
}
free(ivars);
return key;
}
- (NSString *)findNameWithInstance:(UIView *) instance
{
id nextResponder = [self nextResponder];
NSString *name = [self nameWithInstance:instance];
if (!name) {
return [nextResponder findNameWithInstance:instance];
}
if ([name hasPrefix:@"_"]) { //去掉变量名的下划线前缀
name = [name substringFromIndex:1];
}
return name;
}
@end