統計情報(30日間)


最新情報をツイート


人気の投稿

NSLogでblocksのシグネチャ出力を可能にする

このエントリーをはてなブックマークに追加

NSBlockの+descriptionを method_setImplementation()でオーバーライドしてblocksのシグネチャを返すライブラリ。

こういう blocksがあったとする。

NSString * (^someBlock)(NSString *, BOOL, CGRect, float*[30]) = ^(NSString *a, BOOL b, CGRect c, float *d[30]) {
    return @"Some return value";
};
NSLog(@"This is my block! %@", someBlock);

普通NSLogの出力は名前とアドレスしか表示されない。
This is my block! <__NSGlobalBlock__: 0x35c0>

このライブラリを入れるとblocks関数の引数を含むシグネチャが表示できるようになる。
This is my block! <__NSGlobalBlock__: (id (^)(id, char, struct CGRect, float*[30]))>
デバッグ専用。

使う場面は限られてくる一方、NSBlockをカテゴリ拡張したソースコードが興味深い。blocksのデータ構造を知るのに役立つかも。
__attribute__((constructor)) を使って初期化するのか。ふむふむ。

(参考)c++ - How exactly does __attribute__((constructor)) work? - Stack Overflow
http://stackoverflow.com/questions/2053029/how-exactly-does-attribute-constructor-work
1. It's run when a shared library is loaded, typically during program startup.
2. That's how all GCC attributes are; presumably to distinguish them from function calls.
3. GCC-specific syntax.
:





Leave a Reply