开发者

stringWithFormat Bad Access error

开发者 https://www.devze.com 2023-03-30 09:44 出处:网络
Can anyone explain why this code works perfectly: int thumbnailPrefix = trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]);

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

精彩评论

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

关注公众号