使用NSLog进行调试

36
我在Xcode中有以下代码片段:
NSString *digit [[sender titlelabel] text];
NSLog([digit]);

我尝试构建应用程序,并在以下行NSLog([digit]);上收到以下警告消息:

Warning: Format not a string literal and no format arguments

你能告诉我如何解决这个警告信息吗?实际上这个信息是什么意思?

7个回答

60

试试这段代码:

NSString *digit = [[sender titlelabel] text];
NSLog(@"%@", digit);

该消息表示您在使用 digit 变量时语法不正确。如果您没有向其发送任何消息,则不需要任何括号。


嗯,为什么你不能直接传递NSLog(digit)?我认为这将导致一个警告,但是为什么呢? - meow
1
如果你习惯于直接传递NSString对象,那么你会养成这样的习惯。有时候你可能会传递非对象类型,如float或int,这会在记录日志时导致应用程序崩溃。 - Eimantas
2
@mingyeow,传递非文字格式字符串也存在安全风险。许多系统因为调用printf并传递攻击者提供的格式字符串而被攻击。 - bugloaf

30

使用NSLog()如下:

NSLog(@"The code runs through here!");

或者像这样 - 使用占位符:

float aFloat = 5.34245;
NSLog(@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat);

NSLog()中,你可以像这样使用它:+ (id)stringWithFormat:(NSString *)format, ...

float aFloat = 5.34245;
NSString *aString = [NSString stringWithFormat:@"This is my float: %f \n\nAnd here again: %.2f", aFloat, aFloat];

你也可以添加其他占位符:

float aFloat = 5.34245;
int aInteger = 3;
NSString *aString = @"A string";
NSLog(@"This is my float: %f \n\nAnd here is my integer: %i \n\nAnd finally my string: %@", aFloat, aInteger, aString);

5
为什么在digit周围加括号? 应该是这样的:NSLog("%@", digit); 你还漏了第一行中的一个=... 应该是这样的:NSString *digit = [[sender titlelabel] text];

4

使用NSLog的正确方式,正如该警告所解释的那样,是使用格式化程序,而不是传递一个字面值:

不要这样写:

NSString *digit = [[sender titlelabel] text];
NSLog(digit);

使用:

NSString *digit = [[sender titlelabel] text];
NSLog(@"%@",digit);

这样做仍然可以工作,但采用此方法将消除警告。

从 Xcode 中,我注意到 NSLog 期望一个 (NSString * format,...)。那么 *format 究竟代表什么意思呢? - meow
1
@ming yeow - 这是自动完成提示您函数的第一个参数是NSString,format是他们用作示例告诉您它应该是格式化的NSString的名称。 ...是一个逗号分隔的值列表,将传递到格式化的NSString中。例如,如果我放置NSLog(@“You passed%i and%i”,4,5);它会在控制台输出:You passed 4 and 5 - Wayne Hartman

3

类型:BOOL

数据(是/否)或(1/0)

BOOL dtBool = 0;

或者

BOOL dtBool = NO;
NSLog(dtBool ? @"Yes" : @"No");

输出:否

类型:长整型

long aLong = 2015;
NSLog(@"Display Long: %ld”, aLong);

输出:显示长整型数值:2015

long long veryLong = 20152015;
NSLog(@"Display very Long: %lld", veryLong);

输出:显示非常长:20152015

类型:字符串

NSString *aString = @"A string";
NSLog(@"Display string: %@", aString);

输出:显示字符串:一个字符串

类型:浮点数

float aFloat = 5.34245;
NSLog(@"Display Float: %F", aFloat);

输出:显示浮点数:5.342450

类型:整数

int aInteger = 3;
NSLog(@"Display Integer: %i", aInteger);

输出:显示整数:3

NSLog(@"\nDisplay String: %@ \n\n Display Float: %f \n\n Display Integer: %i", aString, aFloat, aInteger);

输出: 字符串:a String

显示浮点数:5.342450

显示整数:3

http://luterr.blogspot.sg/2015/04/example-code-nslog-console-commands-to.html


这是什么样的答案?它只是直接从博客文章复制粘贴过来吗? - Peter Mortensen
链接已损坏。 - Peter Mortensen
这是一个虚假的答案吗?是将问题中的某些内容复制粘贴到搜索引擎中,然后盲目地将第一个结果复制粘贴到答案框中吗?为什么它有4个赞?它如何回答这个问题:“警告:格式不是字符串文字,也没有格式参数...你能建议我如何解决这个警告信息吗?” - Peter Mortensen
另一个答案揭示了http://luterr.blogspot.sg是Luter Rinding自己的博客(现已停用)。 - Peter Mortensen

3
NSLog(@"%@", digit);

控制台显示了什么?


0
NSLog([digit]); // [] are the messages in Objective-C, just like methods or functions in other programming languages

由于您只需要打印“digit”的值

您可以调用 -

NSLog(digit); // A warning would occur - Format string is not a string literal (potentially insecure)

或者

NSLog(@"%@",digit]); // But if you use %@ to reference the object, the warning will go away.

这两种方法都可以工作,但第二种方法是正确的控制台日志记录方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接