I am wondering which of the two following methods is the correct or preferred one to retrieve the superclass of a Class
variable:
Class getSuperclass(Class cls) { return [cls superclass]; }
Class getSuperclass(Class cls) { return class_getSuperclass(cls); }
Well, the docs on
class_getSuperclass()
say this:So, I'd go with door #1.
I am positive they are absolutely identical, meaning that
NSObject
'ssuperclass
is implemented viaclass_getSuperclass
. I am not sure, but I'd bet a beer on it.The accepted answer is technically correct (yes, that's what the docs say), and yet it's an incorrect answer.
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.