Вопрос

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?

Это было полезно?

Решение

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];

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top