首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stringWithFormat访问错误

stringWithFormat访问错误
EN

Stack Overflow用户
提问于 2011-08-23 00:19:43
回答 2查看 1K关注 0票数 0

有没有人能解释为什么这段代码能完美地工作:

代码语言:javascript
复制
int thumbnailPrefix = trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]);

newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",thumbnailPrefix,@"png"];

但是这个代码会导致错误的访问错误吗?

代码语言:javascript
复制
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-23 00:26:22

trunc返回double,而不是int

代码语言:javascript
复制
double trunc(double x);

因此,在第一个代码块中,您可以正确地使用%d格式说明符将其转换为int

在第二个中,它应该是一个%f,或者在它前面有(int)

代码语言:javascript
复制
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
票数 2
EN

Stack Overflow用户

发布于 2011-08-23 00:26:29

你有没有试过像这样来转换trunk()的返回值...

代码语言:javascript
复制
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];

这是虚张声势,但我估计NSString不知道trunc函数的返回类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7150428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档