动态类型

静态类型在编译的时候就能被识别出来。

动态类型就编译器编译的时候是不能被识别的,要等到运行时(run time),即程序运行的时候才会根据语境来识别。

SEL实质是整形

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

2.selector 有2重含义:

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

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

4.场景,当一个类不知到使用哪个对象的方法时,延迟的作用,代理也使用到.

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

在performSelector开头的一些方法允许你延迟后派发指定消息,而且可以将消息(同步或异步的消息)从辅助线程派发到主线程。oc中又三中调用:普通调用,嵌套调用,延迟调用


  IMP

1.IMP是一个返回函数指针的类型,能得到方法的地址。IMP的定义为“id (*IMP) (id, SEL, …)”,也就是一个指向方法的函数指针。

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

#import "Student.h"

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

AppDelegate.m

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];

 

//通过反射射创建对象
    Class cls = NSClassFromString(@"Student");
    id obj3 = [[cls alloc]init];
    
    SEL sel = NSSelectorFromString(@"study");
    [obj3 performSelector:sel withObject:nil];

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值