我在objective-c中有以下代码,并带有警告。我回顾的帖子似乎暗示这不是一个大问题;它是一种自动创建的方法。但在我的情况下,如果应该的话,不会给出结果。我不确定我做错了什么。鉴于这是Objective-C中头几个程序中的一个,我喜欢验证它,所以我可以熟悉正确的语法。下面是代码:目标C未定义的新方法可能是问题
#import <stdio.h>
#import <objc/Object.h>
//---------------interface section-------------
@interface Fraction: Object
{
int numerator;
int denominator;
}
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
@end
//-----------------implementation section-------
@implementation Fraction;
//getters
-(int) numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
//setters
-(void) setNumerator: (int) num
{
numerator = num;
}
-(void) setDenominator: (int) denom
{
denominator = denom;
}
-(void) print
{
printf("The value of the fraction is %i/%i\n", numerator, denominator);
}
@end
//-----------------program section-------------------------
int main (void)
{
Fraction *myFract;
myFract = [Fraction new];
[myFract setNumerator: 1];
[myFract setDenominator: 3];
printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]);
[myFract print];
[myFract free];
return 0;
}
这里是警告消息:
$ gcc -o fraction fraction.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-b
ase -fconstant-string-class=NSConstantString
fraction.m: In function 'main':
fraction.m:43:3: warning: 'Fraction' may not respond to '+new' [enabled by default]
fraction.m:43:3: warning: (Messages without a matching method signature [enabled by default]
fraction.m:43:3: warning: will be assumed to return 'id' and accept [enabled by default]
fraction.m:43:3: warning: '...' as arguments.) [enabled by default]
fraction.m:49:3: warning: 'Fraction' may not respond to '-free' [enabled by default]
这里是输出:
$ ./fraction.exe
The numerator is 0, and the denominator is 0
不知道这是否是相关的,但我使用GNUstep的到编译它。我不知道有一种方法可以在其上运行调试。我尝试了GDB,但没有奏效。顺便说一句,如果我想充分利用此语言的优势,我是否需要Mac环境?这种语言是否有非Mac /苹果应用程序?
有一些非Apple环境应用程序是用Objective-C编写的,但它非常适合Apple技术。 Apple编译器是Objective-C定义的黄金标准。 –
有没有什么办法可以在不购买Apple机器的情况下学习一下库?我喜欢这款产品;我现在不想花钱。 – maverick
新Mac电脑相当昂贵,但您可能能够找到一款相当便宜的Mac电脑。如果您只想学习Objective-C,那么任何可以运行OS X的Mac都应该这样做。可以在非Apple硬件上运行OS X,但这违反了Apple的软件许可协议。有关如何创建“Hackintosh”的信息可以在互联网上随时获得。 –