谈谈对iOS消息的理解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wojiaoweide/article/details/78854341

前言:

OC中的消息有“名称”或者“选择器”,可以接受参数,而且可能还有返回值。
调用方法是OC经常使用的,用OC的术语来说,这叫传递消息。OC是一门动态语言。
对比下C语言,它是一门静态语言,使用“静态绑定”-——在编译器就能决定运行时所应调用的函数。

void printHello() {
    printf("Hello, world!\n");
}
void printGoobye() {
    printf("Goodbye, 222233world!\n");
}

void dothing(int type) {
    void (*fnc) ();
    if (type == 0) {
        printHello();
    } else {
        printGoodbye();
    }
    fnc();

}

编译器在编译代码的时候就已经知道程序中有printHello和printGoodbye这两个函数了,于是会直接生成调用这些函数的指令。而函数地址实际上是硬编码在指令之中的。而

void printHello() {
    printf("Hello, world!\n");
}
void printGoobye() {
    printf("Goodbye, 222233world!\n");
}

void dothing(int type) {
    void (*fnc) ();
    if (type == 0) {
        fnc = printHello;
    } else {
        fnc = printGoodbye;
    }
    fnc();

}

这时得使用“动态绑定”

猜你喜欢

转载自blog.csdn.net/wojiaoweide/article/details/78854341
今日推荐