博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 向类中动态添加方法
阅读量:4646 次
发布时间:2019-06-09

本文共 2340 字,大约阅读时间需要 7 分钟。

@interface TestObject : NSObject-(void)printFirstName:(NSString*)firstName thenSecondName:(NSString*)secondName;-(void)printName:(NSString*)name;-(int)addLeft:(int)left withRight:(int)right;@end
@implementation TestObject-(SEL)oriSelector:(SEL)aSelector{    NSString* v2String=NSStringFromSelector(aSelector);    NSString* oriString=[v2String stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@""];    return NSSelectorFromString(oriString);}-(void)changeInvocation:(NSInvocation*)invocation{    SEL aSelector=[invocation selector];       [invocation setSelector:[self oriSelector:aSelector]];}-(void)v2Method:(NSInvocation*)invocation{    NSLog(@"hello");    [self changeInvocation:invocation];    [invocation invokeWithTarget:self];    NSLog(@"goodbye");}-(void)v3Method:(NSInvocation*)invocation{    NSLog(@"fuck");    [self changeInvocation:invocation];    [invocation invokeWithTarget:self];}-(NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector{    NSString* vString=NSStringFromSelector(aSelector);    NSString* oriString=[vString stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@""];    NSMethodSignature*  signature=[super methodSignatureForSelector:NSSelectorFromString(oriString)];    return signature;}-(void)forwardInvocation:(NSInvocation *)anInvocation{    SEL aSelector=[anInvocation selector];    NSString* aSelectorString=NSStringFromSelector(aSelector);    if([aSelectorString hasPrefix:@"v2"])       [self v2Method:anInvocation];    else if([aSelectorString hasPrefix:@"v3"])        [self v3Method:anInvocation];}-(void)printFirstName:(NSString *)firstName thenSecondName:(NSString *)secondName{    NSLog(@"%@ %@",firstName,secondName);}-(void)printName:(NSString*)name{    NSLog(@"%@",name);}-(int)addLeft:(int)left withRight:(int)right{    int value=left+right;    NSLog(@"%d add %d is %d",left,right,value);    return value;}@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{      TestObject* t=[[TestObject alloc]init];    [t printFirstName:@"your" thenSecondName:@"sister"];        [t v2printFirstName:@"your" thenSecondName:@"sister"];    [t v3printName:@"your sister"];    int a=(int)[t v2addLeft:2 withRight:2];    NSLog(@"%d",a);    //.....}

转载于:https://www.cnblogs.com/eeeyes/archive/2012/04/10/2441272.html

你可能感兴趣的文章
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>
儿子和女儿——解释器和编译器的区别与联系
查看>>
第一阶段冲刺3
查看>>
父类引用指向子类对象
查看>>
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>