Objective-C: preferred way to retrieve the superclass of a Class instance

丶灬走出姿态 提交于 2019-12-03 05:37:52

Well, the docs on class_getSuperclass() say this:

You should usually use NSObject‘s superclass method instead of this function

So, I'd go with door #1.

The accepted answer is technically correct (yes, that's what the docs say), and yet it's an incorrect answer.

[* superclass] only exists for objects that are subclasses of NSObject.

Yes, that's most of the classes you use day to day.

However ... there are many classes you might encounter which are not NSObject subclasses.

Easy example: if you iterate over "all loaded classes" (e.g. using objc_getClassList), then many of the returned classes will crash your app if you use the [Class superclass] method.

I am positive they are absolutely identical, meaning that NSObject's superclass is implemented via class_getSuperclass. I am not sure, but I'd bet a beer on it.

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