【问题标题】:which is the correct specifier to use for long when calling stringWithFormat?调用 stringWithFormat 时,哪个是正确的说明符?
【发布时间】:2011-10-03 20:51:16
【问题描述】:

我的这段代码运行良好,但我不知道它是否是调用 stringWithFormat 的正确方法,因为在文档中 %d 用于 int 并且我传递了很长的内容:

    long seconds = (long)[[NSDate date]timeIntervalSince1970];
    NSString *unixTimestamp = [NSString stringWithFormat:@"date=%d", seconds];

提前致谢!

【问题讨论】:

标签: objective-c ios


【解决方案1】:

试试%ld

 NSString *unixTimestamp = [NSString stringWithFormat:@"date=%ld", seconds];

摘自printf docs(NSString 的 stringWithFormat 格式遵循与 printf 函数相同的标准):

l (ell)
指定以下 d、i、o、u、x 或 X 转换 说明符适用于 long 或 unsigned long 参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2013-03-14
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多