• Runtime(运行时)001-消息机制


    //Person * p = [[Person alloc] init];

    //alloc :分配内存空间  init:初始化对象,属性&方法

    //苹果不建议我们使用Objc_msgSend , 在build-setting 搜索 msg 打开

    //导入 #import <objc/message.h>

    // objc_msgSend        消息发送

    //  objc_getClass           获取类名

    //  sel_registerName     注册方法编号

    //OC 转C  OC 中 1.SEL 方法编号

       Person * p = objc_msgSend(objc_getClass("Person"), sel_registerName("alloc"));

    //p = [p init];

        p = objc_msgSend(p, sel_registerName("init"));

    //[p eatWith:@"汉堡!!"];

    //传参数

        objc_msgSend(p, sel_registerName("eatWith:"),@"汉堡!!");

    //给父类发送消息

         HKPerson * p = [[HKPerson alloc] init];

        //objc_super 结构体指针

        //class_getSuperclass 获取父类类型

        //定义hkSuper 结构体

        struct objc_super hkSuper = {p ,class_getSuperclass(objc_getClass("HKPerson"))};

        //给父类发送消息

        objc_msgSendSuper(&hkSuper, @selector(eatWith:),@"汉堡");

     

    clang编译工具将OC main.m 编译成C++代码

     

    $ clang -rewrite-objc main.m 

    将OC 转 C++

    Person * p = [Person alloc];
    p = [p init];
    int main(int argc, const char * argv[]) {
        /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
            Person * p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc"));
            p = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)p, sel_registerName("init"));
        }
        return 0;
    }

     

     

  • 相关阅读:
    SurfaceView 和 View 区别
    投资学第一章 investments-introduction
    HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
    多个Activity之间的切换与数据交互
    HDU 4715 Difference Between Primes (打表)
    org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the CDATA sectio
    用纯jsp实现用户的登录、注册与退出
    Java单态模式
    植物-蔬菜:刺儿菜
    汉语-词语:生活
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/Runtime_Objc_msgSend.html
Copyright © 2020-2023  润新知