//
// ViewController.m
// TESTzz
//
// Created by point on 2017/4/27.
// Copyright © 2017年 dacai. All rights reserved.
//
#import "ViewController.h"
#import "DCModel.h"
#import <objc/message.h>
static SEL _dcSel;
static SEL _dcSel2;
@interface ViewController ()
{
Class _dcModelClass;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//动态的 添加方法
_dcModelClass = NSClassFromString(@"DCModel"); //这样的写法 不用头文件也可
//[DCModel class] 必须包含头文件
_dcSel = NSSelectorFromString(@"run:");
class_addMethod(_dcModelClass, _dcSel, (IMP)aaa, "v@:@");
//执行方法
static void(*action)(id, SEL,NSNumber *) = (void(*)(id, SEL,NSNumber *))objc_msgSend;
id controller = [[_dcModelClass alloc] init]; //这样执行对象方法
action(controller, _dcSel,@(100));
_dcSel2 = NSSelectorFromString(@"run");
static void(*action2)(id, SEL) = (void(*)(id, SEL))objc_msgSend;
action2(_dcModelClass, _dcSel2); //这个执行的是类方法
}
void aaa(id self, SEL _cmd, NSNumber *meter) {
NSLog(@"跑了%@", meter);
}
@end