如何在 iOS 上使用 NSLog 打印出字符串常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9617301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print out string constant with NSLog on iOS
提问by Borut Tomazin
I have a string constant defined like this:
我有一个这样定义的字符串常量:
#define kMyString @"This is my string text!";
Somewhere in the code I would like to print-out this piece of code with NSLog like that:
在代码的某处,我想用 NSLog 打印出这段代码,如下所示:
NSLog(@"This is it: %@",kMyString);
But get a build error: Expected expression
.
但是得到一个构建错误:Expected expression
.
I have already looked at the Apple's Format Specifiersbut could not figured it out.
我已经查看了 Apple 的格式说明符,但无法弄清楚。
Can someone please explain it to me how to do this?
有人可以向我解释如何做到这一点吗?
Thanks!
谢谢!
回答by sch
You should remove ;
from the definition of kMyString
:
您应该;
从 的定义中删除kMyString
:
#define kMyString @"This is my string text!"
The way you did it is equivalent to:
你这样做的方式相当于:
NSLog(@"This is it: %@", @"This is my string text!";);
回答by Vaibhav Sharma
%@
is for objects. BOOL
is not an object.
On the bases of data type %@
changes as follows
%@
用于对象。BOOL
不是一个对象。
根据数据类型%@
变化如下
For Strings you use %@
For int you use %i
For float you use %f
For double you use %lf
回答by Ankit Srivastava
Remove that semi colon after #define
and use %@
and it will work.
#define
使用后去掉那个分号%@
,它会起作用。