2016-06-24 37 views
1

我在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 /苹果应用程序?

+0

有一些非Apple环境应用程序是用Objective-C编写的,但它非常适合Apple技术。 Apple编译器是Objective-C定义的黄金标准。 –

+0

有没有什么办法可以在不购买Apple机器的情况下学习一下库?我喜欢这款产品;我现在不想花钱。 – maverick

+0

新Mac电脑相当昂贵,但您可能能够找到一款相当便宜的Mac电脑。如果您只想学习Objective-C,那么任何可以运行OS X的Mac都应该这样做。可以在非Apple硬件上运行OS X,但这违反了Apple的软件许可协议。有关如何创建“Hackintosh”的信息可以在互联网上随时获得。 –

回答

1

Object是不推荐的根类。改为使用NSObject。请勿拨打free。如果您正在使用手动内存管理,请使用release。如果你使用ARC,那么不要调用任何东西。

new不必由GNUstep支持,所以您应该使用类allocinit模式。

@interface Fraction: NSObject 
{ 
    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 = [[Fraction alloc] init]; 
    [myFract setNumerator: 1]; 
    [myFract setDenominator: 3]; 

    printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]); 
    [myFract print]; 
    [myFract release]; 
    return 0; 
} 

您还可以通过属性进行简化,以下是带自动引用计数的Obj-C 2.0语法。

@interface Fraction: NSObject 

@property (nonatomic, assign) int numerator; 
@property (nonatomic, assign) int denominator; 

- (void)print; 

@end 

@implementation Fraction 
- (void)print { 
    printf("The value of the fraction is %i/%i\n", self.numerator, self.denominator); 
} 
@end 

int main (void) 
{ 
    Fraction *myFract = [[Fraction alloc] init]; 
    myFract.numerator = 1; 
    myFract.denominator = 3; 

    printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]); 
    [myFract print]; 
    return 0; 
} 

即使使用GNUstep,您也应该使用Clang进行编译。海湾合作委员会不会再做了。

+0

我不认为GNUstep支持ARC,尽管在某些时候他们确实支持标记/扫描垃圾收集。 –

+0

@MarkBessey它确实如此,但与Clang一样,请参阅http://wiki.gnustep.org/index.php/ObjC2_FAQ – Sulthan

+0

哦,很酷。自从我尝试用GNUstep做任何事情之后,我已经有一段时间了。 –