首页 > 其他 > 详细

动态类型

时间:2014-01-26 20:05:50      阅读:500      评论:0      收藏:0      [点我收藏+]

SEL

1.SEL是一个返回方法名的类型,能得到方法名.

2.selector 有2重含义:

  • 在源代码中代表向对象发送消息的方法名称。
  • 在编译后它代表了方法名称被编译后一个统一的标识。

3._cmd当前的方法。NSLog(@"method = %@",NSStringFromSelector(_cmd));

4.[target performSelector:(SEL)];//起运行时调用方法的作用。系统先找方法名,再找地址,然后运行。


  IMP

1.IMP是一个返回函数指针的类型,能得到方法的地址。

2.IMP imp = [target methodForSelector:(SEL))];//指针函数的声明

imp(target,(SEL));//起运行时调用方法的作用。找地址,然后运行。


  Class

 Class定义没定义好的类, id定义定义好的类。


Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject
-(void)study;
@end

Student.m

bubuko.com,布布扣
#import "Student.h"

@implementation Student
-(void)study
{
    NSLog(@"i am student,i study english");
    NSLog(@"%@",NSStringFromSelector(_cmd));
}
@end
bubuko.com,布布扣

AppDelegate.m

bubuko.com,布布扣
Student *s = [[Student alloc]init];
    SEL methodName =@selector(study);
    NSString *string = NSStringFromSelector(methodName);
    NSLog(@"%@",string);
    //SEL method = NSSelectorFromString(string);
    if ([s respondsToSelector:methodName])
    {
        [s performSelector:@selector(study)];//[s study];
    }
    //
    IMP impStudy = [s methodForSelector:@selector(study)];
    impStudy(s,@selector(study));//[s study];
bubuko.com,布布扣

 

 http://www.cnblogs.com/kesalin/archive/2011/08/15/objc_method_base.html

动态类型

原文:http://www.cnblogs.com/huen/p/3531429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!