zoukankan      html  css  js  c++  java
  • 动态类型

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

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

  • 相关阅读:
    《我所理解的生活》—读书总结
    《给你一个团队,你能怎么管?》—读书总结
    关于投资那点儿事
    《30岁前的每一天》—读书总结
    《书都不会读,你还想成功》-读书总结
    解决问题—麦肯锡方法:解决问题的七个步骤
    解决问题—基本流程
    关于接入新浪微博第三方登录
    搭建Spring、Spring MVC、Mybatis和Freemarker
    Eclipse+Maven创建webapp项目<二>
  • 原文地址:https://www.cnblogs.com/huen/p/3531429.html
Copyright © 2011-2022 走看看