我正在写“用 objective c 2.0 编程”这本书,但我不明白为什么这个程序不起作用。基本上我需要构建一个程序来将华氏温度转换为摄氏温度。
我想在没有对象的情况下非常简单地解决它,只使用直接的程序方法,无论如何我遇到的问题是我定义的代表华氏度或摄氏度值的变量的值正在出现随机。
这是我的代码:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
float fahrenheit;
float celciusConverted;
fahrenheit = 27.0;
celciusConverted = ( fahrenheit - 32 ) / 1.8 ;
NSLog(@"%f degrees fahrenheit is equal to %f degrees celcius") , fahrenheit, celciusConverted;
[pool drain];
return 0;
}
最佳答案
您的 NSLog 语句中的右括号放错了位置。它应该就在 ;
你拥有的是
NSLog(@"... %f %f ..."), arg1, arg2;
编译器似乎不够聪明,无法看到 %f
中没有一个有相应的参数,这是 NSLog()
等可变参数函数的常见缺陷。在右括号之后,逗号运算符开始出现,表达式 arg1 和 arg2 什么都不做。
关于objective-c - super 简单的程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1981452/