简单地说,以下代码让我感到困惑,为什么它应该失败。
// 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"];
为什么?
答案 0 :(得分:2)
[[NSDate date] timeIntervalSince1970]
返回的值类型为NSTimeInterval
,类型为typedef double NSTimeInterval;
,您正在打印unsigned int
。将@"%lu%@"
替换为@"%f%@"
即可。