【发布时间】:2010-08-09 18:37:35
【问题描述】:
我想知道将我在 Objective-c 中的这个简单控制台程序连接到一个非常简单的 iPhone 应用程序中是否可行。
由于我没有使用 Interface Builder 的经验,我不确定我需要多长时间才能学会它。
另外,我相信我的代码必须针对某些 iPhone API 进行调整,而不是使用控制台进行输入和输出。
这是程序的 main.m 文件,它的所有代码都存储在其中:
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
#import <stdio.h>
@interface Converter: NSObject
{
//Instance variable which stores converting formula for objects
double formula;
}
//Declare instance methods for setting and getting instance variables
-(void) convert: (double) expression;
-(double) formula;
@end
@implementation Converter;
//Define instance method to set the argument (expression) equal to the instance variable
-(void) convert: (double) expression
{
formula = expression;
}
//Define instance method for returning instance variable, formula
-(double) formula
{
return formula;
}
@end
int main (int argc, char *argv[])
{
//Point two new objects, one for the Fahrenheit conversions and the other for the Celsius conversions, to the Converter class
Converter *fahrenheitConversion = [[Converter alloc] init];
Converter *celsiusConversion = [[Converter alloc] init];
//Declare two double variables holding the user-inputted data, and one integer variable for the if else statement
double fahrenheit, celsius;
int prompt;
NSLog(@"Please press 0 to convert Celsius to Fahrenheit, or 1 to convert Fahrenheit to Celsius\n ");
scanf("%d", &prompt);
if(prompt == 0) {
NSLog(@"Please enter a temperature in Celsius to be converted into Fahrenheit!:\n");
scanf("%lf", &celsius);
if(celsius < -273.15) {
NSLog(@"It is impossible to convert temperatures less than −273.15 degrees Celsius, because this is absolute zero, the coldest possible temperature.");
}
else {
[fahrenheitConversion convert: (((((celsius)*9)/5)+32))];
NSLog(@"%lf degrees Celsius is %lf Fahrenheit", celsius, [fahrenheitConversion formula]);
}
}
else {
NSLog(@"Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%lf", &fahrenheit);
if(fahrenheit < -459.67) {
NSLog(@"It is impossible to convert temperatures less than −459.67 degrees Fahrenheit, because this is absolute zero, the coldest possible temperature.");
}
else {
[celsiusConversion convert: ((((fahrenheit - 32)/9)*5))];
NSLog(@"%lf degrees Fahrenheit is %lf Celsius", fahrenheit, [celsiusConversion formula]);
}
}
return 0;
}
【问题讨论】:
-
您可以,但您必须使用 IB 来创建布局并定义您的 IBActions,这基本上就是您在此处定义的方法。
-
只是想知道,我会不会在 iPhone 下创建一个新的应用程序,类型为 Utility,在我看来有点像小部件应用程序,它有主屏幕,然后你可以把它翻过来到另一边查看一些信息。
标签: iphone c objective-c ios4