为什么说OC是一个动态运行的语言,是因为runtime的设计, 让oc在运行过程中也可以改变执行方法,让我们的编程更加灵活。我们可以通过字符串的形式去调用方法或者属性。
方法调用01
- (void)viewDidLoad {
[super viewDidLoad];
SEL sel = NSSelectorFromString(@"animalRun:andDogName:");
NSString *result = [self performSelector:sel withObject:@"huahua" withObject:@"doudou"];
NSLog(@"%@", result);
}
- (NSString *)animalRun:(NSString *)catName andDogName:(NSString *)dogName{
return [NSString stringWithFormat:@"We begin running, %@, %@", catName, dogName];
}
NSSelector和NSString之间的互换
NSString *funcName = NSStringFromSelector(sel);
SEL sel = NSSelectorFromString(@"animalRun:andDogName:");
方法调用方式02
通过NSInvocation的方式
NSMethodSignature *sign = [[self class] instanceMethodSignatureForSelector:@selector(animalRun:dogName:chickenName:)];
// 注意:生成NSMethodSignature 时有两个方法, 类方法选择methodSignatureForSelector, 实例方法选择:instanceMethodSignatureForSelector 不要搞错了。
// NSMethodSignature *sign = [NSMethodSignature methodSignatureForSelector:@selector(animalRun:dogName:chickenName:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sign];
NSString *catName = @"hua hua";
NSString *dogName = @"dou dou";
NSString *chickenName = @"wu shi";
[invocation setArgument:&catName atIndex:2];
[invocation setArgument:&dogName atIndex:3];
[invocation setArgument:&chickenName atIndex:4];
invocation.selector = @selector(animalRun:dogName:chickenName:);
[invocation invokeWithTarget:self];
// 涉及到C & OC的交互,不写__unsafe_unretained 会导致崩溃, 原因是c语言赋值,
// 不会对returnValue02进行retainCount+1的操作, 导致returnValue02被多次释放导致崩溃。
// __unsafe_unretained表示这个类型不安全,不要再释放。
NSString __unsafe_unretained * returnValue02 = nil;
[invocation getReturnValue:&returnValue02];
NSLog(@"%@", returnValue02);
- (NSString *)animalRun:(NSString *)catName dogName:(NSString *)dogName chickenName:(NSString *)chickenName{
return [NSString stringWithFormat:@"We begin running, %@, %@, %@", catName, dogName, chickenName];
}
方法调用方式03
转换成C语言的调用方式
- (void)viewDidLoad {
[super viewDidLoad];
NSString *funcName = NSStringFromSelector(sel);
SEL animalSel = NSSelectorFromString(@"animalRun:");
IMP imp = [self methodForSelector:animalSel];
NSString * (*func)(id, SEL, id) = (void *)imp;
NSString *result01 = func(self, sel, @[@"hua hua", @"dou dou"]);
NSLog(@"%@", result01);
}
- (NSString *)animalRun:(NSArray *)animals{
NSString *animalsString = [animals componentsJoinedByString:@"&"];
return [NSString stringWithFormat:@"Animals runing:%@", animalsString];
}
方法调用方式04
直接通过objc_msgSend
方法调用
// 注意引入头文件, 否则有警告 #import <objc/message.h>
NSString * (*action)(id, SEL, NSString *, NSString *, NSString *) = (NSString * (*)(id, SEL, NSString *, NSString *, NSString *)) objc_msgSend;
result = action(self, @selector(animalRun:dogName:chickenName:), catName, dogName, chickenName);
NSLog(@"%@", result);
属性
self.userName = @"Jack";
NSString *userName = [self valueForKey:@"userName"];
NSLog(@"%@", userName);
[self setValue:@"Bruce" forKey:@"userName"];
NSLog(@"%@", self.userName);
其它
// 第一个参数:给哪个对象添加关联
// 第二个参数:关联的key,通过这个key获取
// 第三个参数:关联的value
// 第四个参数:关联的策略
objc_setAssociatedObject(self, key, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSClassFromString(@"__NSCFString")
Class toolClass = object_getClass([tool class]);
class_getInstanceMethod
method_exchangeImplementations
....
//等等
PS:祝大家好运
网友评论