gpt4 book ai didi

objective-c - 警告 : Format string is not a string literal (potentially insecure)

转载 作者:太空狗 更新时间:2023-10-30 03:08:45 29 4
gpt4 key购买 nike

我在 NSLog 行中收到警告

Format string is not a string literal(potentially insecure)

来自以下代码

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

请问为什么我把这行改成下面的代码后,警告就没有了?

NSLog(immutableString,nil);

最佳答案

这只是编译器的表达方式,“嘿,你真的知道你在做什么吗?”编译器担心输入字符串可能包含百分号%,而您没有指定相应的参数。显然,根据您提供的代码,情况并非如此,但编译器不够聪明,无法解决这个问题。

通过添加参数(可以是任何内容,包括数字、字符串或 nil),您可以让编译器相信您知道自己在做什么。让编译器满意的另一种方法是使用这样的代码输出字符串。

NSLog( @"%@", immutableString );

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

关于objective-c - 警告 : Format string is not a string literal (potentially insecure),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799731/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com