理解 Objective-C 运行时 [英] Understand Objective-C runtime

查看:77
本文介绍了理解 Objective-C 运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于 Objective-C 运行时的工作原理,所以如果我误解了什么,请评论.

I have read about how Objective-C runtime works, so please comment if I misunderstood something.

假设我有一个名为 Person 的类.该类可能有也可能没有 getSex 方法.

Let's say I have class called Person. That class may or not have method getSex.

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

这里为Person实例分配了内存(isa也被创建,它指向Person类),init用于初始化Person的ivar

Here the memory is allocated for Person instance (isa is created also which points to class Person), init is used to initialize Person ivar's

[p getSex];

这里 objc_msgSend(Person,@selector(getSex) 在 Person 类上被调用.如果 Person 类没有这样的方法,则运行时会在 Person 根类等中查找该方法.当找到方法时,调用 IMP,它是一个指向方法块代码的指针.然后执行该方法.

Here objc_msgSend(Person,@selector(getSex) is called on Person class. If Person class not have such method the runtime look for that method in Person root class and so on. When the method is found IMP is called which is a pointer to method block code. Then the method is executed.

正确吗?

推荐答案

是的,一切都是正确的,除了 init 的行为可能会也可能不会初始化其所有成员变量,以便访问器返回有效结果,尽管这样做是合理的除非另有说明,否则猜测它会初始化属性.

Yes, everything is correct, except the behavior of init may or may not initialize all its member variables such that the accessors return valid results, although it is reasonable to guess that it initializes properties unless told otherwise.

这篇关于理解 Objective-C 运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆