26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSLogで変数名も表示する

Posted at

プリプロセッサディレクティブの文字列化演算子 # を利用する方法がある。
マクロの引数を利用する際に#を付けると、実引数がダブルクォーテーションで囲まれた(C言語の)文字列に置き換わる。

こんな感じでマクロ定義して

#define OBJ_DESCRIPTION(var)	[NSString stringWithFormat:@"%s: %@", #var, var]

こんな感じで使うと

	NSNumber *hoge = @123;
	NSArray *fuga = @[@"foo", @"bar"];
	NSLog(@"%@", OBJ_DESCRIPTION(hoge));
	NSLog(@"%@", OBJ_DESCRIPTION(fuga));

こんな感じのログが出ます

hoge: 123
fuga: (
    foo,
    bar
)
26
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?