使用NSLog进行调试

Zhe*_*hen 26 debugging xcode objective-c nslog

我的Xcode中有以下代码片段:

NSString *digit [[sender titlelabel] text];
NSLog([digit]);
Run Code Online (Sandbox Code Playgroud)

我试图构建应用程序,并获得该行的以下警告消息 NSLog([digit]);

Warning: Format not a string literal and no format arguments
Run Code Online (Sandbox Code Playgroud)

你能告诉我如何解决这个警告信息吗?这个消息究竟意味着什么?

Eim*_*tas 50

试试这段代码:

NSString *digit = [[sender titlelabel] text];
NSLog(@"%@", digit);
Run Code Online (Sandbox Code Playgroud)

该消息表示您使用该digit变量的语法不正确.如果您没有发送任何消息 - 您不需要任何括号.

  • @mingyeow 传递非文字格式字符串也存在安全风险。许多系统都以这种方式受到损害,即通过对攻击者传入的格式字符串调用 printf 。 (2认同)

Fab*_*oni 25

使用NSLog()这样:

NSLog(@"The code runs through here!");
Run Code Online (Sandbox Code Playgroud)

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

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

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];
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)


Goa*_*ine 5

为什么会有括号digit?它应该是

NSLog("%@", digit);

您还缺少=第一行中的...

NSString *digit = [[sender titlelabel] text];