[英]stringWithFormat Bad Access error
Can anyone explain why this code works perfectly: 谁能解释为什么这段代码完美无缺:
int thumbnailPrefix = trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]);
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",thumbnailPrefix,@"png"];
But this code causes a Bad Access error? 但是这段代码导致Bad Access错误?
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
trunc
returns a double
, not an int
. trunc
返回一个double
,而不是int
。
double trunc(double x);
So in the first code block you are converting it to an int
, and correctly using the %d
format specifier. 因此,在第一个代码块中,您将其转换为
int
,并正确使用%d
格式说明符。
In the second, it should be an %f
, or have (int)
in front of it. 在第二个中,它应该是
%f
,或者在它前面有(int)
。
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
Have you tried typecasting the return from trunk() like.... 你有没有尝试过类型转换从trunk()返回....
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
It's a shot in the dark, but I expect NSString doesn't know the return type for the function trunc. 这是一个黑暗中的镜头,但我希望NSString不知道函数trunc的返回类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.