有没有人能解释为什么这段代码能完美地工作:
int thumbnailPrefix = trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]);
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",thumbnailPrefix,@"png"];但是这个代码会导致错误的访问错误吗?
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];发布于 2011-08-23 00:26:22
trunc返回double,而不是int。
double trunc(double x);因此,在第一个代码块中,您可以正确地使用%d格式说明符将其转换为int。
在第二个中,它应该是一个%f,或者在它前面有(int)。
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];发布于 2011-08-23 00:26:29
你有没有试过像这样来转换trunk()的返回值...
newGraph.thumbnailImageName = [NSString stringWithFormat:@"%d.%@",(int)trunc([newGraph.dateCreated timeIntervalSinceReferenceDate]),@"png"];这是虚张声势,但我估计NSString不知道trunc函数的返回类型。
https://stackoverflow.com/questions/7150428
复制相似问题