如何在iOS上使用NSLog打印出字符串常量

时间:2021-01-18 03:37:46

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.

但得到一个构建错误:预期的表达式。

I have already looked at the Apple's Format Specifiers but could not figured it out.

我已经查看了Apple的格式说明符,但无法弄明白。

Can someone please explain it to me how to do this?

有人可以向我解释如何做到这一点?

Thanks!

谢谢!

3 个解决方案

#1


37  

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!";);

#2


2  

Remove that semi colon after #define and use %@ and it will work.

在#define之后删除该半冒号并使用%@,它将起作用。

#3


2  

%@ 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

#1


37  

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!";);

#2


2  

Remove that semi colon after #define and use %@ and it will work.

在#define之后删除该半冒号并使用%@,它将起作用。

#3


2  

%@ 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