【问题标题】:Why is my NSSMutableString potentially insecure?为什么我的 NSMutableString 可能不安全?
【发布时间】:2012-09-11 17:11:50
【问题描述】:

为什么我的NSMutableString 可能不安全?我搜索了这个,但找不到任何东西。

int hour = [number intValue] / 3600; 

NSMutableString *time = [[NSMutableString alloc] initWithString:@""];

if (hour < 9) {
    [time appendFormat:@"0"];
    [time appendFormat:[NSMutableString stringWithFormat:@"%d:", hour]];
}

它有什么问题?这是我第一次看到这个。

【问题讨论】:

标签: objective-c cocoa-touch nsmutablestring


【解决方案1】:

改变这个:

[time appendFormat:[NSMutableString stringWithFormat:@"%d:", hour]];

到这里:

[time appendFormat:@"%d:", hour];

appendFormat 方法希望您传递具有格式的字符串,而不是 NSMutableString。这张截图显示了它:

【讨论】:

  • 这当然是答案。但你可能想稍微解释一下。 :)
  • 非常感谢,我现在知道哪里出错了; )
【解决方案2】:

这没有回答这里提出的问题,但看起来提供的代码正在尝试重新发明现有的格式字符串选项。

[NSMutableString stringWithFormat:@"%02d:", hour] 将打印一个带前导零的两位整数值。

【讨论】:

  • 没错,但我认为这应该是一条评论,因为它没有回答问题。
猜你喜欢
  • 2016-01-02
  • 2017-06-01
  • 2012-07-18
  • 2013-04-28
  • 2019-10-27
  • 2019-08-31
  • 2011-06-16
  • 2018-05-14
  • 2021-02-26
相关资源
最近更新 更多