NSLog - 如何打印对象名称? [英] NSLog - How to print object name?

查看:120
本文介绍了NSLog - 如何打印对象名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑,

NSString *myString = @"Welcome";

NSLog(@"%@",myString);

将在控制台中打印欢迎

我可以打印字符串,如 myString:Welcome

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

我的意思是,我可以获取对象名称( myString )以及对象值​​(欢迎 )?

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

推荐答案

使用以下代码:

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

NSString *myString=@"Welcome";

stringVariable(myString); 

注意:一般原则是当你把#放在前面时在#define体内的参数中,预处理器将其替换为传递给宏的精确表达式的C字符串。传递变量名称时,您将获得该名称。

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.

这篇关于NSLog - 如何打印对象名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆