开发者

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?

newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",trunc([newGraph开发者_Go百科.dateCreated timeIntervalSinceReferenceDate]),@"png"];


trunc returns a double, not an 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.

In the second, it should be an %f, or have (int) in front of it.

newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];


Have you tried typecasting the return from trunk() like....

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜