NSStringWithFormat崩溃

时间:2012-04-14 09:54:34

标签: ios nsstring stringwithformat

简单地说,以下代码让我感到困惑,为什么它应该失败。

// This line works
NSString * string = [NSString stringWithFormat:@"%lu",[[NSDate date] timeIntervalSince1970]];

// This line works
string = [NSString stringWithFormat:@"%@",@"somestring"];

// This line generates and EXEC_BAD_ACCESS error
string = [NSString stringWithFormat:@"%lu%@",[[NSDate date] timeIntervalSince1970],@"somestring"];

为什么?

1 个答案:

答案 0 :(得分:2)

[[NSDate date] timeIntervalSince1970]返回的值类型为NSTimeInterval,类型为typedef double NSTimeInterval;,您正在打印unsigned int。将@"%lu%@"替换为@"%f%@"即可。