ObjecTips

Swift & Objective-C で iOS とか macOS とか

クラス階層を調べる

Private なクラスに出会った時に、そのクラスの superClass がさらに Private だったりする事がある。
その時にさらに superClass に遡って、、とやるのも面倒なので一気にクラス階層を出力できるようにする。

実装方法は以下の記事と同じく koze.hatenablog.jp

#import <objc/runtime.h>
で Runtime API を import して以下のように実装する。


これを例えば UITableView で使ってみる。

    Class cls = [UITableView class];
    PrintClassHierarchy(cls);

実行した出力結果が以下

// Class Hierarchy of UITableView
UITableView : UIScrollView : UIView : UIResponder : NSObject

Runtime API 便利。