使用 NSLog 进行调试

发布于 2024-10-28 05:18:37 字数 295 浏览 4 评论 0原文

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

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

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

Warning: Format not a string literal and no format arguments

您能告诉我如何解决此警告吗信息?该消息的实际含义是什么?

I have the following code snippet in my Xcode:

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

I tried to build the application and am getting the following warning message for the line NSLog([digit]);

Warning: Format not a string literal and no format arguments

Can you advise me how I can resolve this warning message? What does the message actually mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

罗罗贝儿 2024-11-04 05:18:37

尝试这段代码:

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

该消息意味着您使用 digit 变量的语法不正确。如果您不向其发送任何消息 - 您不需要任何括号。

Try this piece of code:

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

The message means that you have incorrect syntax for using the digit variable. If you're not sending it any message - you don't need any brackets.

风苍溪 2024-11-04 05:18:37

像这样使用 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);

Use NSLog() like this:

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

Or like this - with placeholders:

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

In NSLog() you can use it like + (id)stringWithFormat:(NSString *)format, ...

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

You can add other placeholders, too:

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);
忘羡 2024-11-04 05:18:37

为什么数字两边有括号?
它应该是

NSLog("%@", digital);

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

NSString *digit = [[发件人标题标签]文本];

Why do you have the brackets around digit?
It should be

NSLog("%@", digit);

You're also missing an = in the first line...

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

世界等同你 2024-11-04 05:18:37

正如警告试图解释的那样,使用 NSLog 的正确方法是使用格式化程序,而不是传递文字:

而不是:

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

使用:

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

它仍然可以按照第一种方式工作,但这样做会摆脱的警告。

The proper way of using NSLog, as the warning tries to explain, is the use of a formatter, instead of passing in a literal:

Instead of:

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

Use:

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

It will still work doing that first way, but doing it this way will get rid of the warning.

背叛残局 2024-11-04 05:18:37
NSLog(@"%@", digit);

控制台显示什么?

NSLog(@"%@", digit);

what is shown in console?

挽梦忆笙歌 2024-11-04 05:18:37

类型:BOOL

DATA (YES/NO) OR(1/0)

BOOL dtBool = 0;

OR

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

输出: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);

输出:显示字符串:a 字符串

类型:浮点型

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

输出: isplay 浮点: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);

输出:
字符串:字符串

显示浮点数:5.342450

显示整数:3

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

type: BOOL

DATA (YES/NO) OR(1/0)

BOOL dtBool = 0;

OR

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

OUTPUT: NO

type: Long

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

OUTPUT: Display Long: 2015

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

OUTPUT: Display very Long: 20152015

type: String

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

OUTPUT: Display String: a String

type: Float

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

OUTPUT: isplay Float: 5.342450

type: Integer

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

OUTPUT: Display Integer: 3

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

OUTPUT:
String: a String

Display Float: 5.342450

Display Integer: 3

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

淡淡绿茶香 2024-11-04 05:18:37
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.

这两种方法都可以工作,但第二种方法是登录到控制台的正确方法。

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

Since you just need to print the value of 'digit'

Either you can call -

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

OR

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

Both the methods will work but the second one is the right way of logging to console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文