質問

I would like to NSLog the current class file that the simulator loads, in order to make sure the class file loaded currently. What parameter should I code?

Is it possible?

Thanks.

役に立ちましたか?

解決

You can use the preprocessor directive __FILE__ to access the file path of the currently executing code. This flag is set at compile-time, but for usage in an NSLog, this shouldn't matter.

You can use it like this:

NSLog("Log called from file %s", __FILE__);
==> Log called from file /Developer Projects/Objective-C/Mac/test/test/AppDelegate.m

There are also other preprocessor variables you can use, such as __LINE__ and __PRETTY_FUNCTION__

You can define it in a preprocessor macro, and use it as such:

#define NSFileLog(format, ...) NSLog(@"%s:%d :: " format, __FILE__, __LINE__, ##__VA_ARGS__)

...

NSFileLog(@"Test Log");
==> /Developer Projects/Objective-C/Mac/test/test/AppDelegate.m:20 :: Test Log
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top