/* Basic Messaging Primitives
*
* On some architectures, use objc_msgSend_stret for some struct return types.//发送返回值类型为结构体的消息
* On some architectures, use objc_msgSend_fpret for some float return types. //发送返回值类型为浮点类型的消息
* On some architectures, use objc_msgSend_fp2ret for some float return types.//发送返回值类型为浮点类型的消息
*
* These functions must be cast to an appropriate function pointer type
* before being called.
*/
void objc_msgSend(void /* id self, SEL op, ... */ )
<span style="font-size:18px;">在ViewController.m中:
//请选择合适的objc_msgSend写出一下程序.
//1.创建并初始化对象
Person *person1 = ((Person *(*)(id,SEL))objc_msgSend)((id)[Personclass],@selector(alloc));
person1 = ((Person *(*)(id,SEL))objc_msgSend)((id)person1,@selector(init));
// <==>Person *person1 = [[Person alloc] init];
//2.发送无参数无返回值消息
((void *(*)(id,SEL))objc_msgSend)((id)self,@selector(noRetNoArg));
//3.带参数不带返回值消息
((void *(*)(id,SEL,NSString*))objc_msgSend)((id)self,@selector(noRetWithArg:),@"string type");
//4.带返回值不带参数消息
BOOL hasRet = ((BOOL *(*)(id,SEL))objc_msgSend)((id)self,@selector(hasRetNoArg));
//5.带参数带返回值的消息
BOOL hasRet2 = ((BOOL *(*)(id,SEL,NSString *))objc_msgSend)((id)self,@selector(hasRetWithArg:),@"string type");
//6.动态添加方法再调用
BOOL retAddM =class_addMethod([selfclass], NSSelectorFromString(@"cStyleFunc"), (IMP)cStyleFunc,"i@:r^vr^v");
//i@:r^vr^v 描述:其中i代表返回类型int,@代表参数接收者,:代表SEL,r^v是const void *
if (retAddM) {
int retI = ((int (*)(id,SEL,constvoid*,constvoid*))objc_msgSend)((id)self,NSSelectorFromString(@"cStyleFunc"),"arg1","arg2");
NSLog(@"retI:%d",retI);
}
//7.带浮点返回值的消息
float fRet = ((float (*)(id,SEL,NSString *))objc_msgSend_fpret)((id)self,@selector(hasfRetWithArg:),@"123");
//8.带结构体返回值的消息
((CGRect (*)(id,SEL))objc_msgSend_stret)((id)self,@selector(hasStructRetNoArg));
- (void)noRetNoArg{
NSLog(@"发送无参数无返回值消息:%s ,参数:%@,返回值:%@",__FUNCTION__,[NSNullnull],[NSNullnull]);
}
- (void)noRetWithArg:(NSString *)arg{
NSLog(@"带参数不带返回值消息:%s ,参数:%@,返回值:%@",__FUNCTION__,arg,[NSNullnull]);
}
- (BOOL)hasRetNoArg{
BOOL hasRet =YES;
NSLog(@"带返回值不带参数消息:%s ,参数:%@,返回值:%@",__FUNCTION__,[NSNullnull],hasRet?@"YES":@"NO");
return hasRet;
}
- (BOOL)hasRetWithArg:(NSString *)arg{
BOOL hasRet =YES;
NSLog(@"带参数带返回值的消息:%s ,参数:%@ ,返回值:%@",__FUNCTION__,arg,hasRet?@"YES":@"NO");
return hasRet;
}
- (float)hasfRetWithArg:(NSString *)arg{
float fRet = [argfloatValue];
NSLog(@"带浮点返回值的消息:%s ,参数:%@ ,返回值:%f",__FUNCTION__,arg,fRet);
return fRet;
}
- (CGRect)hasStructRetNoArg{
CGRect rect =CGRectMake(0,0, 100,100);
NSLog(@"带结构体返回值的消息:%s ,参数:%@ ,返回值:%@",__FUNCTION__,[NSNullnull],NSStringFromCGRect(rect));
return rect;
}
int cStyleFunc(id receiver,SEL sel,void const *arg1,voidconst *arg2){
NSLog(@"动态添加方法再调用:%s,参数1: %@, 参数2: %@",
__FUNCTION__,
[NSStringstringWithUTF8String:arg1],
[NSStringstringWithUTF8String:arg1]);
return1;
}</span>
2016-03-24 21:49:19.251objc_msgSendDemo[917:84605]发送无参数无返回值消息:-[ViewController noRetNoArg] ,参数:<null>,返回值:<null>
2016-03-24 21:49:19.252objc_msgSendDemo[917:84605]带参数不带返回值消息:-[ViewController noRetWithArg:] ,参数:string type,返回值:<null>
2016-03-24 21:49:19.252objc_msgSendDemo[917:84605]带返回值不带参数消息:-[ViewController hasRetNoArg] ,参数:<null>,返回值:YES
2016-03-24 21:49:19.253objc_msgSendDemo[917:84605]带参数带返回值的消息:-[ViewController hasRetWithArg:] ,参数:string type ,返回值:YES
2016-03-24 21:49:19.253objc_msgSendDemo[917:84605]动态添加方法再调用:cStyleFunc,参数1: arg1,参数2: arg1
2016-03-24 21:49:19.253objc_msgSendDemo[917:84605] retI:1
2016-03-24 21:49:19.254objc_msgSendDemo[917:84605]带浮点返回值的消息:-[ViewController hasfRetWithArg:] ,参数:123 ,返回值:123.000000
2016-03-24 21:49:19.254objc_msgSendDemo[917:84605]带结构体返回值的消息:-[ViewController hasStructRetNoArg] ,参数:<null> ,返回值:{{0, 0}, {100, 100}}
文章参考:http://www.henishuo.com/runtime-objc-msgsend/