0

Given a class Car that extends the class Vehicle that extends NSObject.

How is it possible to get programmatically the parent class Vehicle from the child class Car in Objective-C?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Giorgio
  • 13,129
  • 12
  • 48
  • 75

2 Answers2

4

Use class_getSuperclass() function.

Class parentClass = class_getSuperclass([Car class]);

You need to import "objc/runtime.h" in order to use this function.

Also there is superclass method on NSObject

Class parentClass =  [Car superclass];
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
1

Just send the message superclass. You can then use the macro NSStringFromClass() if you need the name.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55