解决编译时出现的警告:format string is not a string literal (potentially insecure)

   转载自:http://www.cocoachina.com/bbs/read.php?tid=87645



       在Xcode 4.2(iOS 5)之前,我猜大家都和我一样很喜欢下面的调试输出写法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     NSString *str = @ "Attention" ;
     
     // first warning
     NSLog (str);
     
     // second warning
     NSLog ([str stringByAppendingFormat:@ ", %@" , @ "Xcode 4.2 do not support this format!" ]);
     
     // third warning
     NSLog ([ NSString stringWithFormat:@ "%@, Xcode 4.2 do not support this format!" , @ "Attention" ]);
     
     // fourth warning
     NSException *exception = [ NSException exceptionWithName:@ "Attention"
                                                      reason:@ "Xcode 4.2 do not support this format!"
                                                    userInfo: nil ];
     
     NSLog (exception);


但是在Xcode 4.2(iOS 5)之后,貌似苹果更新的编译器,出了支持ARC的Apple LLVM compiler 3.0。然后我发现每次编译,以前的这些输出都会产生一个warning(警告,黄色三角形)。
在StackOverflow和iPhone Dev SDK查找相关答案之后,发现在最新版的编译器里面NSLog为了安全,只接受格式化的字符串,因为NSLog底层也是用printf来格式化输出的。
所以上面的写法都会给出警告,可以把上面的写法修改为以下合法模式:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
     NSString *str = @ "Attention" ;
     
     // first warning
     NSLog (str);                           // warning
     NSLog (@ "%@" , str);         // solution
     NSLog (str, nil );                    // solution
     
     // second warning
     NSLog ([str stringByAppendingFormat:@ ", %@" , @ "Xcode 4.2 do not support this format!" ]);                         // warning
     NSLog (@ "%@" , [str stringByAppendingFormat:@ ", %@" , @ "Xcode 4.2 do not support this format!" ]);         // solution
     NSLog ([str stringByAppendingFormat:@ ", %@" , @ "Xcode 4.2 do not support this format!" ], nil );                    //solution
     
     // third warning
     NSLog ([ NSString stringWithFormat:@ "%@, Xcode 4.2 do not support this format!" , @ "Attention" ]);             // warning
     NSLog (@ "%@, Xcode 4.2 do not support this format!" , @ "Attention" );                                                                 // solution
     NSLog ([ NSString stringWithFormat:@ "%@, Xcode 4.2 do not support this format!" , @ "Attention" ], nil );       // solution
     
     // fourth warning
     NSException *exception = [ NSException exceptionWithName:@ "Attention"
                                                      reason:@ "Xcode 4.2 do not support this format!"
                                                    userInfo: nil ];
     
     NSLog (exception);                         // warning
     NSLog (@ "%@" , exception);       // solution

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值