Understand Objective-C runtime

邮差的信 提交于 2019-12-06 13:14:09

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.

There's one piece that's slightly off.

The call will actually be one of these three:

objc_msgSend(p, @selector(getSex))
objc_msgSend_fpret(p, @selector(getSex))
objc_msgSend_stret(p, @selector(getSex))

One difference here is that the first argument is to the object and not to the class.

Also, since you didn't share what the getSex method returns, it's not possible for us to know whether it will be one of the fpret/stret versions or not. If the method returns a double (on certain platforms), the fpret version will be used. If the method returns a structure value (on certain platforms), then the stret version will be used. All others will use the standard version. All of this is platform dependent in many different ways.

As the others said, allocation will create an object with all instance variables set to 0/NULL and a valid isa pointer as well. Initialization methods may or may not update the instance variables with something meaningful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!