NSLog - How to print object name?

依然范特西╮ 提交于 2019-12-31 00:30:28

问题


Consider,

NSString *myString = @"Welcome";

NSLog(@"%@",myString);

will print Welcome in console.

Can I print the string like "myString: Welcome"?

I mean, can I get the object name("myString") along with object value("Welcome")?


回答1:


Use the following code:

#define stringVariable(x) NSLog( @"%s:%@",#x, x) 

NSString *myString=@"Welcome";

stringVariable(myString); 

Note: The general principle is that when you put a # in front of an argument within the body of a #define, the preprocessor replaces it with a C string of the exact expression passed to the macro. When you pass a variable name, you'll get that name.



来源:https://stackoverflow.com/questions/29893406/nslog-how-to-print-object-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!