我在objective-c中有以下代码,并带有警告。我审查的帖子似乎暗示这不是一个大问题;它是一种自动创建的方法。在我的情况下,如果应该不给出结果。我不确定我做错了什么。鉴于这是我正在做的Objective-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应用程序?
答案 0 :(得分:1)
Object
是一个已弃用的根类。请改用NSObject
。不要致电free
。如果您使用的是手动内存管理,请使用release
。如果您使用的是ARC,请不要拨打任何电话。
new
不必支持 GNUstep
,因此您应使用课程alloc
和init
模式。
@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进行编译。 GCC不会再做了。