stringWithFormat 访问错误
任何人都可以解释为什么这段代码可以完美地工作:
int thumbnailPrefix = trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]);
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",thumbnailPrefix,@"png"];
但是这段代码会导致错误访问错误?
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];
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.dateCreated timeIntervalSinceReferenceDate]),@"png"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
trunc
返回一个double
,而不是int
。因此,在第一个代码块中,您将其转换为
int
,并正确使用%d
格式说明符。在第二个中,它应该是
%f
,或者前面有(int)
。trunc
returns adouble
, not anint
.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.您是否尝试过对 trunk() 的返回进行类型转换,例如......
这是在黑暗中拍摄,但我希望 NSString 不知道函数 trunc 的返回类型。
Have you tried typecasting the return from trunk() like....
It's a shot in the dark, but I expect NSString doesn't know the return type for the function trunc.