NSObject *obj = @"A string or other object.";
NSLog([NSString stringWithFormat:@"%@",obj]);// 有警告
NSLog([NSString stringWithFormat:@"%@",obj],nil);// 解决方案
为何如此?
解答=>上面可以直接写成:
NSLog(@"A string or other object.");
//或者
NSObject *obj = @"A string or other object.";
NSLog(@"%@",obj);
//没有必要再调用 NSString的 stringWithFormat: 方法
===============================================================
注:警告信息:“Format string is not a string literal (potentially insecure)”说明NSLog要求的参数为字面量,不可为NSString* 类型,如:
NSString* s=...;
NSLog(s) //警告
//-------------------------
NSLog([NSString stringWithFormat...]);//依旧警告
本文解释了在Objective-C中使用NSLog函数时常见的警告问题“Format string is not a string literal (potentially insecure)”。通过示例展示了如何正确地使用NSLog打印对象,并指出直接使用字符串字面量或%@格式化符来打印NSObject实例是更简洁的方法。

9428

被折叠的 条评论
为什么被折叠?



