警告:格式字符串不是字符串文字(可能不安全) [英] Warning : Format string is not a string literal (potentially insecure)

查看:158
本文介绍了警告:格式字符串不是字符串文字(可能不安全)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NSLog行中得到警告

I get the warning in the NSLog line

Format string is not a string literal(potentially insecure)

根据以下代码

NSMutableString  *MarqueeMessage = [[NSMutableString alloc]init];
[MarqueeMessage appendString:@"Abc"];
NSString *immutableString = MarqueeMessage;
NSLog(immutableString);

请问为什么在将行更改为以下代码后,警告消失了?

May I ask why after I changed the line into the following code, the warning is gone?

NSLog(immutableString,nil);

推荐答案

这只是编译器的说法,嘿,您真的知道您在做什么吗?"编译器担心输入字符串可能包含百分号%,并且您尚未指定相应的参数.显然,根据您提供的代码不是这种情况,但是编译器不够聪明,无法弄清楚.

That's just the compiler's way of saying, "Hey, do you really know what you're doing?" The compiler is concerned that the input string may contain a percent character %, and you haven't specified the corresponding argument. Obviously, that's not the case based on the code you've provided, but the compiler isn't smart enough to figure that out.

通过添加一个参数(可以是数字,字符串或nil,可以是任何东西),您可以说服编译器您知道自己在做什么.使编译器满意的另一种方法是使用这样的代码输出字符串.

By adding an argument (which could be anything including a number, a string, or nil) you convince the compiler that you know what you're doing. The alternative way to make the compiler happy is to output the string with code like this.

NSLog( @"%@", immutableString );

此方法的优点是字符串中的意外格式说明符(例如%s)不会引起任何问题.

The advantage of this method is that unexpected format specifiers in the string (e.g. %s) won't cause any problems.

这篇关于警告:格式字符串不是字符串文字(可能不安全)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆