动态类型

来源:互联网 发布:linux root鉴定错误 编辑:程序博客网 时间:2024/06/08 17:10

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

动态类型就编译器编译的时候是不能被识别的,要等到运行时(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

 

0 0